Class: Errsight::CaptureMiddleware

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

Overview

Catches exceptions raised by any inner middleware/controller before ActionDispatch::DebugExceptions converts them into an HTTP response, so

errors that occur outside the controller (e.g. ActiveRecord::Migration

CheckPending, Rack middleware, routing) are still reported with a real backtrace. Inserted after DebugExceptions in the stack so the dev/error page still renders normally on re-raise.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CaptureMiddleware

Returns a new instance of CaptureMiddleware.



9
10
11
# File 'lib/errsight/capture_middleware.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/errsight/capture_middleware.rb', line 13

def call(env)
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  # Push a per-request scope so set_user/set_tag/add_breadcrumb calls made
  # by app code are isolated to this request. Capture happens *inside* the
  # block so the exception is tagged with the request's scope before pop.
  Errsight.with_scope do
    begin
      @app.call(env)
    rescue Exception => exception
      capture(exception, env, started)
      raise
    end
  end
ensure
  Thread.current[:errsight_captured_exceptions] = nil
end