From: Christian Neukirchen Date: 2006-05-08T22:57:21+09:00 Subject: Re: REXML exception rescuing Eli Bendersky writes: > Ross Bamford wrote: >> On Mon, 2006-05-08 at 03:21 +0900, Eli Bendersky wrote: >>> When REXML throws ParseException, a whole load information is spilled >>> into msg. Although this is useful for debugging, it hardly is an option >>> for a "real-life" application that just has to notify the user of an >>> error in his .xml file in the friendliest way possible. >>> >>> How can I get just the "malformed XML: missing tag start" part (plus the >>> line in the file) without all the stack trace and the information that >>> comes after it, which for some reason is repeated twice. Is this >>> possible without deconstructing the raised msg ? >> >> This is probably a bit naive but maybe will be a start: >> >> begin >> doc = REXML::Document.new File.new('bad.xml') >> rescue REXML::ParseException => ex >> puts "Failed: #{ex.message[/^.*$/]} (#{ex.message[/Line:\s\d+/]})" >> end > > Ross, > > Thanks for your suggestion, it was clear to me that this is possible, > but I seek a more 'sane' way of doing it. What if REXML changes the > format of the message slightly in the next version - the regexes won't > match any longer. I don't see why there can't be a short way to print a > message that is useful to an end user. > > > REXML is praised as *the* XML parsing library of Ruby and is part of the > standard library, surely someone noticed its useless error reporting ?! Why didn't you bother to just look in the code? :-) [~/src/ruby-1.8.4/lib/rexml/parseexception.rb:] module REXML class ParseException < RuntimeError ... def position @source.current_line[0] if @source and defined? @source.current_line and @source.current_line end def line @source.current_line[2] if @source and defined? @source.current_line and @source.current_line end def context @source.current_line end end end -- Christian Neukirchen http://chneukirchen.org