Skip to content

Instantly share code, notes, and snippets.

@gretel
Last active Aug 29, 2015
Embed
What would you like to do?
tiny Rack middleware to have Yell::Logger log exceptions nicely.
# tom@jitter.eu https://gist.github.com/gretel/388f14f1de38174ea790 09/2014 MIT License
# Rack middleware to Yell (https://github.com/rudionrails/yell) exceptions
# config.ru:
# require 'yell_exceptions'
# use Rack::YellExceptions
require 'yell'
module Rack
class YellExceptions
attr_accessor :str
def initialize(app, log = nil, str = nil)
@app = app
log ||= yeller
fail unless log.kind_of? Yell::Logger
@logger = log
str ||= "%s\n%s\n%s"
@str = str
end
def call(env)
begin
response = @app.call(env)
rescue Object => e
except(e, env)
raise
end
response
end
private
def except(e, env = nil)
@logger.fatal(sprintf(@str, e, e.backtrace.join("\n"), env.to_a[0].to_s)) # todo: test
end
def yeller
return Object.logger if Object.logger.kind_of? Yell::Logger
Yell.new :stdout, name: self.class
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment