It would be nice if Exception class could be extended by #format method (or method with some more suitable name), which would provide the same formatting as is standard output of Ruby exception, e.g.
boomtest.rb:18:in go': boom (RuntimeError) from boomtest.rb:34:in foo'
from boomtest.rb:36
Currently, this formatting is done by error_print function from eval_error.c. Unfortunately this method is just Ruby's internal.
Please note that there exists exception_string gem, which add similar method [1] to the Exception class.
It would be nice if Exception class could be extended by #format method (or method with some more suitable name), which would provide the same formatting as is standard output of Ruby exception, e.g.
ruby -v set to ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]
nobu (Nobuyoshi Nakada) wrote:
Why and how would it be nice?
I'm trying to develop Ruby support for ABRT [1]. The idea is, that I will provide at_exit handler and in this handler, I need to report the backtrace. I'd like to report the same backtrace as Ruby reports when running from command line. Since the Exception cannot provide this format, I need to provide some custom functionality to get the backtrace formatted as Ruby does.
I am probably not the first one who needs it, otherwise the gem would not exist.
It is actually surprising that the formatting of exception is not provided by the Exception class. I realise that it is probably due to some historic reasons, but once the Exception has its #format method, it could be used in place of error_print().
Would it be possible to make the #format method public? And if I may be a bit daring, some documentation would be nice. I appreciate your effort. Thank you.