Class: Checkend::Integrations::Rack::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/checkend/integrations/rack.rb

Overview

Rack middleware for capturing unhandled exceptions and request context.

Examples:

Basic usage

use Checkend::Integrations::Rack::Middleware

With Sinatra

class MyApp < Sinatra::Base
  use Checkend::Integrations::Rack::Middleware
end

Constant Summary collapse

FILTERED_HEADERS =

Headers that should be filtered from request data

%w[
  HTTP_COOKIE
  HTTP_AUTHORIZATION
  HTTP_X_API_KEY
  HTTP_X_AUTH_TOKEN
].freeze
EXCLUDED_HEADERS =

Headers to exclude entirely (not useful for debugging)

%w[
  HTTP_HOST
  HTTP_CONNECTION
  HTTP_ACCEPT_ENCODING
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



32
33
34
# File 'lib/checkend/integrations/rack.rb', line 32

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/checkend/integrations/rack.rb', line 36

def call(env)
  # Store request data for potential error reporting
  request_data = extract_request_data(env)
  Thread.current[:checkend_request] = request_data

  # Set request context
  Checkend.set_context(
    request_id: env['HTTP_X_REQUEST_ID'] || env['action_dispatch.request_id'],
    method: env['REQUEST_METHOD'],
    path: env['PATH_INFO']
  )

  begin
    @app.call(env)
  rescue Exception => e # rubocop:disable Lint/RescueException
    # Report the exception to Checkend
    notify_exception(e, request_data)

    # Re-raise so the error propagates to the app's error handler
    raise
  ensure
    # Clean up thread-local data
    Thread.current[:checkend_request] = nil
    Checkend.clear!
  end
end