Skip to content

Instantly share code, notes, and snippets.

@arnklint
Forked from TomV/exception_mailer.rb
Created August 4, 2011 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnklint/1124778 to your computer and use it in GitHub Desktop.
Save arnklint/1124778 to your computer and use it in GitHub Desktop.
Simple Sinatra extension to catch exceptions and do some notifiyin (not working yet.)
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
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
# 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