[Ironruby-core] hosting: howto get the nice ruby backtrace on exception thrown from ruby?

Meinrad Recheis meinrad.recheis at gmail.com
Tue Feb 17 17:36:31 EST 2009


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<RubyArray>("__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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20090217/a3b15b35/attachment.html>


More information about the Ironruby-core mailing list