From: Matthew Kerwin Date: 2013-07-05T23:32:23+09:00 Subject: Re: How do I rescue a program exception in an at_exit block? It's a pure, unadulterated hack, but you can "exec" something at the end of your at_exit block, to replace the ruby process with something else, thus avoiding the final error report. at_exit do error = $! if error #... end exec 'echo -n ""' end raise 'badness' On 6 July 2013 00:08, Josh Cheek wrote: > I was thinking about how bad I am at reading error messages, and realized it > could be useful to catch the exception and write it in a way that was easier > for me to find the relevant information. So, came up with this proof of > concept: > > at_exit do > error = $! > if error > pink, normal, red, green = "\e[35m", "\e[0m", "\e[31m", "\e[32m" > > puts > puts "#{pink}#{error.class}" > puts "#{red}#{error.message}#{normal}" > puts > puts error.backtrace.map { |line| > line.sub(%r([^/:]*(?=:))) { |match| "#{green}#{match}#{normal}" } > .sub(%r((?<=:)\d+(?=:))) { |match| "#{pink}#{match}#{normal}" } > } > end > end > > > The problem, though, is that $! is still set after this block runs, and so > Ruby still spits out its own exception message afterwards. I don't know how > to stop it from doing this. > > I tried setting $!, but it's a read-only variable. Tried raising and > rescuing another exception, which cleared $! for the rest of that at_exit > block, but it was still around in the next at_exit block, and so Ruby still > printed it out. Not sure what else to try. > > -Josh -- Matthew Kerwin, B.Sc (CompSci) (Hons) http://matthew.kerwin.net.au/