-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handles errors in bandit plug dispatch
- Loading branch information
Showing
5 changed files
with
154 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule Tower.BanditExceptionHandler do | ||
require Logger | ||
|
||
@handler_id __MODULE__ | ||
|
||
def attach do | ||
:telemetry.attach( | ||
@handler_id, | ||
[:bandit, :request, :exception], | ||
&__MODULE__.handle_event/4, | ||
_handler_config = [] | ||
) | ||
end | ||
|
||
def detach do | ||
:telemetry.detach(@handler_id) | ||
end | ||
|
||
def handle_event( | ||
[:bandit, :request, :exception], | ||
_event_measurements, | ||
event_metadata, | ||
_handler_config | ||
) do | ||
handle_event_metadata(event_metadata) | ||
end | ||
|
||
defp handle_event_metadata(%{ | ||
kind: _kind, | ||
exception: %{ | ||
__struct__: Plug.Conn.WrapperError, | ||
kind: :error, | ||
reason: reason, | ||
stack: stacktrace, | ||
conn: conn | ||
}, | ||
stacktrace: stacktrace | ||
}) do | ||
Exception.normalize(:error, reason, stacktrace) | ||
|> Tower.handle_exception(stacktrace, plug_conn: conn) | ||
end | ||
|
||
defp handle_event_metadata(event_metadata) do | ||
Logger.warning( | ||
"UNHANDLED BANDIT REQUEST EXCEPTION with event_metadata=#{inspect(event_metadata, pretty: true)}" | ||
) | ||
|
||
:ignored | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters