class Minicron::Hub::ExceptionHandling

Based on hawkins.io/2013/06/error-handling-in-sinatra-apis/

Public Class Methods

new(app) click to toggle source
# File lib/minicron/hub/app.rb, line 127
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/minicron/hub/app.rb, line 147
def call(env)
  begin
    @app.call env
  rescue ActiveRecord::RecordNotFound => e
    handle_exception(env, e, 404)
  rescue Exception => e
    handle_exception(env, e, 500)
  end
end
handle_exception(env, e, status) click to toggle source
# File lib/minicron/hub/app.rb, line 131
def handle_exception(env, e, status)
  if Minicron.config['trace']
    env['rack.errors'].puts(e)
    env['rack.errors'].puts(e.backtrace.join("\n"))
    env['rack.errors'].flush
  end

  # Display the error message
  hash = { :error => e.to_s }

  # Display the full trace if tracing is enabled
  hash[:trace] = e.backtrace if Minicron.config['trace']

  [status, { 'Content-Type' => 'application/json' }, [hash.to_json]]
end