Class: Alplus::RackMiddleware

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

Overview

Rack middleware: captures any exception that propagates up through the app, then re-raises so the host's own error page / handler still runs unchanged. Installed automatically by the Rails railtie; usable directly in a plain Rack app (use Alplus::RackMiddleware).

Also resets Scope to a fresh, empty one for the duration of the request (issue #17): a thread-pool server (Puma, Passenger) reuses OS threads across requests, so without this a set_user/set_tag call in request A would otherwise still be visible to request B if it happens to land on the same thread.

Session lifecycle (issue #12): opens a fresh request-scoped Session (Session.with_clean_session, the same reused-thread guard as Scope above) and closes it (Alplus.close_session) once @app.call returns OR raises. Unlike sdks/elixir/lib/alplus_sdk/plug.ex (which cannot rescue a downstream plug's exception — see that module's moduledoc), Rack middleware genuinely wraps the rest of the stack in one method call, so rescue/ensure here is the complete, reliable crash signal: no separate telemetry hook is needed.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.



26
27
28
# File 'lib/alplus/rack_middleware.rb', line 26

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



30
31
32
# File 'lib/alplus/rack_middleware.rb', line 30

def call(env)
  Scope.with_clean_scope { Session.with_clean_session { call_app(env) } }
end