Skip to content

Commit

Permalink
Unwrap Plug.Conn.WrapperErrors raised by Plug
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrudel committed Dec 18, 2024
1 parent 4015c86 commit a053c1c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/bandit/pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ defmodule Bandit.Pipeline do
) :: {:ok, Bandit.HTTPTransport.t()} | {:error, term()}
defp handle_error(kind, error, stacktrace, transport, span, opts, conn \\ nil)

defp handle_error(
:error,
%Plug.Conn.WrapperError{} = error,
_stacktrace,
transport,
span,
opts,
conn
) do
# Unwrap the inner error and handle it
handle_error(error.kind, error.reason, error.stack, transport, span, opts, conn)
end

defp handle_error(:error, %type{} = error, stacktrace, transport, span, opts, _conn)
when type in [
Bandit.HTTPError,
Expand Down
30 changes: 30 additions & 0 deletions test/bandit/http1/request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,36 @@ defmodule HTTP1RequestTest do
def known_crasher(_conn) do
raise SafeError, "boom"
end

defmodule Router do
use Plug.Router
plug(Plug.Logger)
plug(:match)
plug(:dispatch)

get "/" do
# Quiet the compiler
_ = conn
raise "boom"
end
end

test "it should unwrap Plug.Conn.WrapperErrors and handle the inner error", context do
context =
context
|> http_server(plug: Router)
|> Enum.into(context)

output =
capture_log(fn ->
{:ok, response} = Req.get(context.req, url: "/", base_url: context.base)
assert response.status == 500
Process.sleep(100)
end)

refute output =~ "(Plug.Conn.WrapperError)"
assert output =~ "[error] ** (RuntimeError) boom"
end
end

describe "invalid requests" do
Expand Down

0 comments on commit a053c1c

Please sign in to comment.