Skip to content

Instantly share code, notes, and snippets.

@nitrogenlogic
Created June 22, 2011 22:07
Show Gist options
  • Save nitrogenlogic/1041374 to your computer and use it in GitHub Desktop.
Save nitrogenlogic/1041374 to your computer and use it in GitHub Desktop.
[non-functional] Monkey patch exception logging into async_sinatra (quick workaround)
# 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
@nitrogenlogic
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment