Skip to content

Commit

Permalink
fix: logs normalized erlang error when unwrapping Plug.Conn.WrapperError
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Jan 13, 2025
1 parent 177b053 commit 618c918
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lib/bandit/pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,15 @@ defmodule Bandit.Pipeline do
) :: {:ok, Bandit.HTTPTransport.t()} | {:error, term()}
defp handle_error(:error, %Plug.Conn.WrapperError{} = error, _, transport, span, opts, metadata) do
# Unwrap the inner error and handle it
handle_error(error.kind, error.reason, error.stack, transport, span, opts, metadata)
handle_error(
error.kind,
Exception.normalize(:error, error.reason, error.stack),
error.stack,
transport,
span,
opts,
metadata
)
end

defp handle_error(:error, %type{} = error, stacktrace, transport, span, opts, metadata)
Expand Down
12 changes: 9 additions & 3 deletions test/bandit/http1/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ defmodule HTTP1PlugTest do
get "/" do
# Quiet the compiler
_ = conn
raise "boom"
1 = 0
end
end

Expand All @@ -134,9 +134,15 @@ defmodule HTTP1PlugTest do
SimpleHTTP1Client.send(client, "GET", "/hello_world", ["host: banana"])
assert SimpleHTTP1Client.connection_closed_for_reading?(client)

assert_receive {:log, %{level: :error, msg: {:string, msg}}}, 500
assert_receive {:log, %{level: :error, msg: {:string, msg}, meta: meta}}, 500
refute msg =~ "(Plug.Conn.WrapperError)"
assert msg =~ "** (RuntimeError) boom"
assert msg =~ "** (MatchError)"

assert %{
domain: [:elixir, :bandit],
crash_reason: {%MatchError{}, [_ | _] = _stacktrace},
conn: %Plug.Conn{}
} = meta
end
end

Expand Down

0 comments on commit 618c918

Please sign in to comment.