Matt Armstrong
2/8/2005 11:45:00 PM
Henrik Ronellenfitsch <hen_WEG_damit_rik@searinox.de> writes:
> Hello,
> is it possible to let ruby print error messages directly as text to the
> cgi-output, like the way php prints it's error messages?
> It's kind of impractical to always have to check the apache error log.
>
> Thanks,
> Henrik
This approach will cause any exception in my main() function to be
printed out for the user to see. Syntax errors won't be caught, of
course, but even that can be handled by making the main .cgi script a
tiny wrapper with all the logic in a separate file that is loaded
within the begin...rescue...end. I stick my whole script in a timeout
block, since I've had odd problems cause a script to hang forever,
causing bad load on the HTTP server.
def exception_string(cgi, exception)
s1 = cgi.h1{"Exception:"} +
CGI::escapeHTML(exception.inspect) +
cgi.h1{"Backtrace:"}
s2 = ""
exception.backtrace.each { |line|
s2 = s2 + CGI::escapeHTML(line) + cgi.br
}
return s1 + s2
end
cgi = CGI::new('html4Tr')
begin
timeout(60 * 10) {
main(cgi)
}
rescue Exception => exception
cgi.out {
cgi.html {
cgi.head {
cgi.title { "#{TITLE}: Exception" }
} +
cgi.body {
exception_string(cgi, exception)
}
}
}
end
--
matt