Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch exceptions, raise Plug.Conn.WrapperError #278

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
jobs:
test:
name: Elixir ${{matrix.elixir}} / OTP ${{matrix.otp}}
runs-on: ubuntu-latest
runs-on: ubuntu-20.04

strategy:
matrix:
Expand Down
68 changes: 37 additions & 31 deletions lib/absinthe/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -286,37 +286,43 @@ defmodule Absinthe.Plug do
"""
@spec call(Plug.Conn.t(), map) :: Plug.Conn.t() | no_return
def call(conn, config) do
config = update_config(conn, config)
{conn, result} = conn |> execute(config)

case result do
{:input_error, msg} ->
conn
|> encode(400, error_result(msg), config)

{:ok, %{"subscribed" => topic}} ->
conn
|> subscribe(topic, config)

{:ok, %{data: _} = result} ->
conn
|> encode(200, result, config)

{:ok, %{errors: _} = result} ->
conn
|> encode(200, result, config)

{:ok, result} when is_list(result) ->
conn
|> encode(200, result, config)

{:error, {:http_method, text}, _} ->
conn
|> encode(405, error_result(text), config)

{:error, error, _} when is_binary(error) ->
conn
|> encode(500, error_result(error), config)
try do
config = update_config(conn, config)
{conn, result} = conn |> execute(config)

case result do
{:input_error, msg} ->
conn
|> encode(400, error_result(msg), config)

{:ok, %{"subscribed" => topic}} ->
conn
|> subscribe(topic, config)

{:ok, %{data: _} = result} ->
conn
|> encode(200, result, config)

{:ok, %{errors: _} = result} ->
conn
|> encode(200, result, config)

{:ok, result} when is_list(result) ->
conn
|> encode(200, result, config)

{:error, {:http_method, text}, _} ->
conn
|> encode(405, error_result(text), config)

{:error, error, _} when is_binary(error) ->
conn
|> encode(500, error_result(error), config)
end
catch
kind, reason ->
stack = __STACKTRACE__
Plug.Conn.WrapperError.reraise(conn, kind, reason, stack)
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/lib/absinthe/plug/document_provider_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule Absinthe.Plug.DocumentProviderTest do

test "cannot process without any document providers" do
opts = Absinthe.Plug.init(schema: TestSchema, document_providers: [])
assert_raise RuntimeError, fn -> request(opts) end
assert_raise Plug.Conn.WrapperError, fn -> request(opts) end
end

defp request(opts) do
Expand Down
15 changes: 15 additions & 0 deletions test/lib/absinthe/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,21 @@ defmodule Absinthe.PlugTest do
end
end

test "raises wrapped error with plugged conn on uncaught exceptions" do
opts = Absinthe.Plug.init(schema: TestSchema)
body = %{query: "{ exceptionRaising }"}

exception =
assert_raise Plug.Conn.WrapperError, fn ->
conn(:post, "/", body)
|> put_req_header("content-type", "application/json")
|> plug_parser
|> Absinthe.Plug.call(opts)
end

assert exception.conn.body_params == %{"query" => "{ exceptionRaising }"}
end

def test_before_send(conn, val) do
# just for easy testing
send(self(), {:before_send, val})
Expand Down
6 changes: 6 additions & 0 deletions test/support/test_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ defmodule Absinthe.Plug.TestSchema do
raise "complex string must not be resolved"
end
end

field :exception_raising, :string do
resolve fn _, _ ->
raise "uncaught exception"
end
end
end

subscription do
Expand Down