-
-
Save sinefunc/397311 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'sinatra/base' | |
module Sinatra | |
# module to catch Sinatra errors and send a email | |
module ExceptionMailer | |
def initialize(app) | |
@app = app | |
# set parameters here.. | |
yield self if block_given? | |
end | |
def call(env) | |
status, headers, body = | |
begin | |
@app.call(env) | |
rescue => error_raised | |
# TODO don't allow exceptions from send_notification to | |
# propogate | |
send_notification error_raised, env | |
raise | |
end | |
[status, headers, body] | |
end | |
private | |
def send_notification(exception, env) | |
puts "SENDING NOTIFICATION for :#{exception}" | |
end | |
end | |
register ExceptionMailer | |
end | |
This file contains hidden or 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
Request "/" should get 'hello mate..' but instead, the server shows an error, and doesn't respond to request. | |
== Sinatra/0.9.5 has taken the stage on 4567 for development with backup from Thin | |
>> Thin web server (v1.2.7 codename No Hup) | |
>> Maximum connections set to 1024 | |
>> Listening on 0.0.0.0:4567, CTRL+C to stop | |
SENDING NOTIFICATION for :private method `call' called for nil:NilClass | |
!! Unexpected error while processing request: undefined method `each' for nil:NilClass |
This file contains hidden or 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
# simple script to test catching a sinatra exception | |
require 'rubygems' | |
require 'sinatra' | |
require 'exception_mailer' | |
configure do | |
set :raise_errors, true | |
# enable :raise_errors | |
# set :show_exceptions, false | |
end | |
get '/' do | |
"hello mate! Try <a href='/boom'>this link</a>" | |
end | |
get '/boom' do | |
raise 'uh oh.. mate! we got a problem. ' | |
end | |
error do | |
'Sorry there was a nasty error - ' + env['sinatra.error'].name | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment