# Hosting: howto get the nice ruby backtrace on exception thrown from ruby?

**URL:** <https://www.ruby-forum.com/t/hosting-howto-get-the-nice-ruby-backtrace-on-exception-thrown-from-ruby/160585>\
**Category:** IronRuby\
**Created:** [February 18, 2009, 12:29am UTC](https://www.ruby-forum.com/t/hosting-howto-get-the-nice-ruby-backtrace-on-exception-thrown-from-ruby/160585 "2009-02-18T00:29:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [February 18, 2009, 12:29am UTC](https://www.ruby-forum.com/t/hosting-howto-get-the-nice-ruby-backtrace-on-exception-thrown-from-ruby/160585/1 "2009-02-18T00:29:53Z")

</div>

hello,

I am now currently using this ugly hack to get a reasonable (let’s say  
user  
friendly) exception backtrace from an error in executed code:

```
    public object Execute(string code, ScriptScope scope)
    {
        code = @"begin

```

\_=(  
“+code+@”  
)  
rescue Exception  
self. **message** =$!.message.to\_clr\_string  
self. **backtrace** = $!.backtrace  
throw  
end";  
object result = null;  
try  
{  
LogManager.GetLogger(“Ruby”).Debug("\r\n" + code);  
result = m\_engine.Execute(code, scope);

```
        }
        catch (Exception e)
        {
            var backtrace =

```

scope.GetVariable(“ **backtrace** ”);  
string msg = scope.GetVariable(“ **message** ”)  
+"\r\n" + string.Join("\r\n", backtrace.Select(s =\>  
s.ToString()).ToArray());

```
            LogManager.GetLogger("Ruby").Error(msg);
        }
        return result;
    }

```

Even though it seems to work well I am absolutely not satisfied with it.  
Is  
there a better way to do this?  
– henon

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [February 18, 2009, 10:29am UTC](https://www.ruby-forum.com/t/hosting-howto-get-the-nice-ruby-backtrace-on-exception-thrown-from-ruby/160585/2 "2009-02-18T10:29:43Z")

</div>

I’ve given it some thought. I would like to avoid wrapping the code in  
“begin - rescue” and instead catch the exception in c#. Then just send  
the  
ruby methods “message” and “backtrace” to the Ruby-exception-object from  
C#.  
How would that be done?  
Any other hints?-- Henon

On Tue, Feb 17, 2009 at 11:36 PM, Meinrad R.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [February 19, 2009, 3:46am UTC](https://www.ruby-forum.com/t/hosting-howto-get-the-nice-ruby-backtrace-on-exception-thrown-from-ruby/160585/3 "2009-02-19T03:46:48Z")

</div>

You can use ExceptionService:

```
    var engine = Ruby.CreateEngine();
    try {
        engine.Execute(@"

```

def foo  
goo  
end

def goo  
raise ‘hello’  
end

foo  
");  
} catch (Exception e) {  
var exceptionService =  
engine.GetService();  
string message, typeName;  
exceptionService.GetExceptionMessage(e, out message, out  
typeName);  
Console.WriteLine(message);  
Console.WriteLine(typeName);  
Console.WriteLine(exceptionService.FormatException(e));  
}

The current implementation is not ideal. If you’re missing some features  
on the service let us know. We might consider adding them.

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Meinrad  
Recheis  
Sent: Wednesday, February 18, 2009 1:25 AM  
To: ironruby-core  
Subject: Re: [Ironruby-core] hosting: howto get the nice ruby backtrace  
on exception thrown from ruby?

I’ve given it some thought. I would like to avoid wrapping the code in  
“begin - rescue” and instead catch the exception in c#. Then just send  
the ruby methods “message” and “backtrace” to the Ruby-exception-object  
from C#. How would that be done?  
Any other hints?  
– Henon  
On Tue, Feb 17, 2009 at 11:36 PM, Meinrad R.  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
hello,

I am now currently using this ugly hack to get a reasonable (let’s say  
user friendly) exception backtrace from an error in executed code:  
public object Execute(string code, ScriptScope scope)  
{  
code = @“begin  
\_=(  
“+code+@”  
)  
rescue Exception  
self. **message** =$!.message.to\_clr\_string  
self. **backtrace** = $!.backtrace  
throw  
end”;  
object result = null;  
try  
{  
LogManager.GetLogger(“Ruby”).Debug(“\r\n” + code);  
result = m\_engine.Execute(code, scope);

```
        }
        catch (Exception e)
        {
            var backtrace = 

```

scope.GetVariable(“ **backtrace** ”);  
string msg = scope.GetVariable(“ **message** ”)  
+“\r\n” + string.Join(“\r\n”, backtrace.Select(s =\>  
s.ToString()).ToArray());

```
            LogManager.GetLogger("Ruby").Error(msg);
        }
        return result;
    }

```

Even though it seems to work well I am absolutely not satisfied with it.  
Is there a better way to do this?  
– henon

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [February 19, 2009, 10:06am UTC](https://www.ruby-forum.com/t/hosting-howto-get-the-nice-ruby-backtrace-on-exception-thrown-from-ruby/160585/4 "2009-02-19T10:06:56Z")

</div>

Thanks Thomas, thats very good. As for a suggestion about  
ExceptionService  
features: It would be quite useful to retrieve the exception backtrace  
as  
string[] in order to be able format it to one’s needs. For instance, I  
like  
using the VisualStudio error parser to be able to jump from the  
backtrace in  
the output right into the right line in the source file (see my blog  
post:  
[Navigating in Exception Stack Traces in Visual C-Sharp - eqqon](http://www.eqqon.com/index.php/Navigating_in_Exception_Stack_Traces_in_Visual_C-Sharp)).  
To do that, currently, I would need to parse the formatted backtrace and  
re-format it. No big deal. Just would be nice to have.

Cheers,  
– henon

On Thu, Feb 19, 2009 at 3:30 AM, Tomas M. \<

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [February 19, 2009, 6:11pm UTC](https://www.ruby-forum.com/t/hosting-howto-get-the-nice-ruby-backtrace-on-exception-thrown-from-ruby/160585/5 "2009-02-19T18:11:08Z")

</div>

I completely agree with that. I think we should add a method  
GetStackTrace() that returns an instance of some StackTrace class, which  
is an array of StackFrames, where StackFrame is some struct/class that  
contains method name, file name and line number.

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Meinrad  
Recheis  
Sent: Thursday, February 19, 2009 1:06 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] hosting: howto get the nice ruby backtrace  
on exception thrown from ruby?

Thanks Thomas, thats very good. As for a suggestion about  
ExceptionService features: It would be quite useful to retrieve the  
exception backtrace as string[] in order to be able format it to one’s  
needs. For instance, I like using the VisualStudio error parser to be  
able to jump from the backtrace in the output right into the right line  
in the source file (see my blog post:  
[Navigating in Exception Stack Traces in Visual C-Sharp - eqqon](http://www.eqqon.com/index.php/Navigating_in_Exception_Stack_Traces_in_Visual_C-Sharp)).  
To do that, currently, I would need to parse the formatted backtrace and  
re-format it. No big deal. Just would be nice to have.

Cheers,  
– henon

On Thu, Feb 19, 2009 at 3:30 AM, Tomas M.  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\>  
wrote:

You can use ExceptionService:

```
    var engine = Ruby.CreateEngine();

    try {

        engine.Execute(@"

```

def foo

goo

end

def goo

raise ‘hello’

end

foo

");

```
    } catch (Exception e) {

        var exceptionService = 

```

engine.GetService();

```
        string message, typeName;

        exceptionService.GetExceptionMessage(e, out message, out 

```

typeName);

```
        Console.WriteLine(message);

        Console.WriteLine(typeName);

        Console.WriteLine(exceptionService.FormatException(e));

    }

```

The current implementation is not ideal. If you’re missing some features  
on the service let us know. We might consider adding them.

Tomas

From:  
removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
On Behalf Of Meinrad R.  
Sent: Wednesday, February 18, 2009 1:25 AM  
To: ironruby-core  
Subject: Re: [Ironruby-core] hosting: howto get the nice ruby backtrace  
on exception thrown from ruby?

I’ve given it some thought. I would like to avoid wrapping the code in  
“begin - rescue” and instead catch the exception in c#. Then just send  
the ruby methods “message” and “backtrace” to the Ruby-exception-object  
from C#. How would that be done?  
Any other hints?

– Henon

On Tue, Feb 17, 2009 at 11:36 PM, Meinrad R.  
\<removed\_email\_address@domain.invalid[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:

hello,

I am now currently using this ugly hack to get a reasonable (let’s say  
user friendly) exception backtrace from an error in executed code:

```
    public object Execute(string code, ScriptScope scope)

    {

        code = @"begin

```

\_=(

“+code+@”

)

rescue Exception

self. **message** =$!.message.to\_clr\_string

self. **backtrace** = $!.backtrace

throw

end";

```
        object result = null;

        try

        {

            LogManager.GetLogger("Ruby").Debug("\r\n" + code);

            result = m_engine.Execute(code, scope);

        }

        catch (Exception e)

        {

            var backtrace = 

```

scope.GetVariable(“ **backtrace** ”);

```
            string msg = scope.GetVariable<string>(" __message__") 

```

+“\r\n” + string.Join(“\r\n”, backtrace.Select(s =\>  
s.ToString()).ToArray());

```
            LogManager.GetLogger("Ruby").Error(msg);

        }

        return result;

    }

```

Even though it seems to work well I am absolutely not satisfied with it.  
Is there a better way to do this?  
– henon
