Class: OopsieExceptions::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
# File 'lib/oopsie_exceptions/middleware.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/oopsie_exceptions/middleware.rb', line 9

def call(env)
  Context.clear

  begin
    request_context = Context.from_rack_env(env)
    Context.merge(request_context)

    if OopsieExceptions.configuration.context_builder
      extra = OopsieExceptions.configuration.context_builder.call(env)
      Context.merge(extra) if extra.is_a?(Hash)
    end

    response = @app.call(env)

    if response[0].to_i >= 500
      Context.merge(response_status: response[0].to_i)
    end

    response
  rescue Exception => exception
    Context.merge(response_status: 500)
    OopsieExceptions.report(exception, handled: false)
    raise
  ensure
    Context.clear
  end
end