Skip to content

Commit

Permalink
Add the action instance to the request environment (#446)
Browse files Browse the repository at this point in the history
This is useful for third party instrumentation tools like AppSignal.

---------

Co-authored-by: Tim Riley <[email protected]>
  • Loading branch information
tombruijn and timriley authored Nov 4, 2024
1 parent b084e07 commit 5d8d610
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/hanami/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ def call(env)
_handle_exception(request, response, exception)
end

# Before finishing, put ourself into the Rack env for third-party instrumentation tools to
# integrate with actions
env[ACTION_INSTANCE] = self

finish(request, response, halted)
end

Expand Down
4 changes: 4 additions & 0 deletions lib/hanami/action/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,9 @@ class Action
# @since 2.0.0
# @api private
DEFAULT_CHARSET = "utf-8"

# @since 2.2.0
# @api private
ACTION_INSTANCE = "hanami.action_instance"
end
end
3 changes: 2 additions & 1 deletion spec/support/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,8 @@ def handle(_req, _res)

class ParamsAction < Hanami::Action
def handle(req, res)
res.body = req.params.to_h.inspect
params = req.params.to_h
res.body = params.inspect
end
end

Expand Down
8 changes: 8 additions & 0 deletions spec/unit/hanami/action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
expect(response.body).to eq(["Hi from TestAction!"])
end

it "sets the action instance on the request environment object" do
action = CallAction.new
env = {}
action.call(env)

expect(env[Hanami::Action::ACTION_INSTANCE]).to eq(action)
end

context "when an exception isn't handled" do
it "should raise an actual exception" do
expect { UncheckedErrorCallAction.new.call({}) }.to raise_error(RuntimeError)
Expand Down

0 comments on commit 5d8d610

Please sign in to comment.