From: Eli Bendersky Date: 2006-05-09T05:04:35+09:00 Subject: Re: REXML exception rescuing Eli Bendersky wrote: >>> 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. Since I found no better way around it, and REXML is pretty much the only powerful pure-Ruby XML lib out there, I had to turn to the ugly side, handcrafting the sanest explanation possible out of REXML parse errors: module REXML class ParseException < RuntimeError # Try to make the sanest possible explanation of a parse error, # suitable for display to users # def explain str = self.to_s # If it's the broken 'missing tag start' error, # output the "unconsumed chars" as they serve as # a good hint to the location of the problem # if str.match(/missing tag start/) if str.match(/Last \d+ unconsumed characters:\n([^\n]+)\n/) "near #{$1}" else "(unknown)" end # Otherwise it's a normal error, so return the description # else if str.match(/REXML::ParseException: (.*)$/) "#{$1} (Line #{self.line})" else "(unknown)" end end end end end Tell me what you think. Could have I extracted more information ? My knowledge of REXML is still basic so I'm surely missing some quirks. -- Posted via http://www.ruby-forum.com/.