Class: ExceptionHandling

Inherits:
Object
  • Object
show all
Defined in:
lib/exception_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ExceptionHandling

Returns a new instance of ExceptionHandling.



2
3
4
# File 'lib/exception_handler.rb', line 2

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/exception_handler.rb', line 6

def call(env)
  begin
    @app.call env
  rescue => ex
    env['rack.errors'].puts ex
    env['rack.errors'].puts ex.backtrace.join("\n")
    env['rack.errors'].flush

    hash = { :message => ex.to_s }
    hash[:backtrace] = ex.backtrace[0..5]

    [500, {'Content-Type' => 'application/json'}, [JSON.pretty_generate(hash)]]
  end
end