Created
June 22, 2011 22:07
-
-
Save nitrogenlogic/1041374 to your computer and use it in GitHub Desktop.
[non-functional] Monkey patch exception logging into async_sinatra (quick workaround)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Monkey patch exception logging into async_sinatra | |
# | |
# This is a workaround for the lack of error handling/logging on asynchronous | |
# requests (e.g. aget, apost) when using async_sinatra 0.5.0, sinatra 1.2.6, | |
# thin 1.2.11, and rack 1.3.0. | |
# | |
# See http://stackoverflow.com/questions/6427033/how-do-i-log-asynchronous-thinsinatrarack-requests | |
module Sinatra::Async | |
alias :oldaroute :aroute | |
def aroute *args, &block | |
oldaroute(*args) { |*a| | |
begin | |
block.call *a | |
rescue Exception => e | |
status 500 | |
puts "Asynchronous exception:" | |
puts "#{e.class} - #{e.message}: \n #{e.backtrace.join("\n ")}\n" | |
ahalt e | |
end | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work because the execution context of the block is changed, preventing access to methods like content_type and status. Some metaprogramming would be required to bind the block call to the right context, as is done in the async_sinatra code.