Producing an error message for a run-time ruby error
-
I am putting this here in case it helps someone someday.
When you get an error while running a ruby script, it appears with a traceback in the ruby console. However, if someone is running your script with the ruby console closed, then there is often no warning that an error has occurred.
This code lets you put a begin/rescue/end sequence around almost anything, and give the end user a useful error message.
You do not need to put the begin/rescue/end sequence near the error. It will still report a useful error message even if the error is quite a ways into the calling sequence.
If you use these in several places in your code, ruby will execute the one closest to where the error occurred. You only need to define do_rescue() once, and you can call it as needed.
def do_rescue(e, who) smess = sprintf("%s ERROR during; %s;\n\t%s\n\nCalled by;\n\t%s" + "\n\n\tPlease report this error to support@xxx.com\n\n", e.class.name, who, e.message, e.backtrace.join("\n\t")) UI.messagebox(smess, MB_OK, "XXX Error") end#def def test_error1 begin test_error2 # code which may contain an error rescue Exception => e do_rescue(e, "Test Error") # put a message here to describe the process with the error end#begin end#def def test_error2 test_error3 end#def def test_error3 xx = yy.to_i # this gives a run time error end#def
-
I think it's better to use MB_MULTILINE instead of MB_OK, because this way the user can select and copy the message and the email address.
azuby
-
Thanks - just the hint I needed.
I was trying to figure out how to let the user copy the message to an email.
-
I think we should think about a "service library" instead of letting the single coders (re)define their own methods. Because some of them may think of changing the behaviour of the method which may influence other plugins. And with such a library Sketchup only loads and interpretes the code once and will start faster.
azuby
-
Especially with the method I defined for this sample, do_rescue(), it would be a problem if everyone started adding one to his/her ruby application - because SketchUp would use the last one which was loaded.
In my actual application, I added do_rescue() to a class, which (hopefully) will be unique. But we have about 10 separate applications, and each one has to contain its own copy of do_rescue() and about 10 other routines which are shared by all the applications. And it is a problem how to keep them synchronized. (One problem here, is that I haven't found a good way to use the same source file in several applications - defining the same routines, but in different classes)
One problem with a "service library", is that application developers would add it to their installer, and, (depending on how well they wrote their installer), people who installed older applications might overwrite new versions of the service library, and/or developers might make changes to the service library which won't work with older applications. (We all try not to do this, but all to often things creep into newer versions of a library which won't work with the older versions.)
Still, it would be good to find a way to enhance SketchUp by adding some common ruby routines which we could all use. Maybe we can get SketchUp to appoint (and/or fund) someone to create a "ruby library" for SketchUp which everyone would use, and which would provide its own installer for version control.
-
def test_error1 begin test_error2 # code which may contain an error rescue Exception => e do_rescue(e, "Test Error") end#begin end#def
can be even reduced to
def test_error1 test_error2 # code which may contain an error rescue Exception => e do_rescue(e, "Test Error") end
But what really bothers me is that there is no option to make Ruby console pop up when there are new messages and stay hidden otherwise.
As for now I guess I would just keep a file with
Sketchup.send_action("showRubyPanel;")
in the Plugins folders while doing active development.
Advertisement