Reputation: 75840
There are a lot of guides available for handling error tuples in absinthe but next to zero for exceptions.
This is important because there are always unforseen issues which might raise an exception and return a response that will not conform to the graphql response/error spec. This can be especially problematic when GraphQL clients like apollo automatically batch requests, and an exception in one query will crash the whole BEAM web process causing all queries to fail.
My first thought was to wrap the resolvers in a try/rescue
block using middleware and the only two links I came across, also suggested a similar approach:
Elixir Forum: How to use Absinthe.MiddleWare to catch exception?
Ben Wilson, one of the creators of Absinthe, recommends replacing the Resolution
middleware with a custom one that executes the resolver in a try
block
This would not handle exceptions in other middleware (but maybe that's how it should be)
Blog Post: Handling Elixir Exceptions in Absinthe using Middleware
Absinthe.Middleware
behaviour specMy approach is a bit inspired from the blog post, but I've tried to follow the behaviour and use middleware tuple spec instead of anonymous functions:
Middleware Definition:
defmodule MyApp.ExceptionMiddleware do
@behaviour Absinthe.Middleware
@default_error {:error, :internal_server_error}
@default_config []
@spec wrap(Absinthe.Middleware.spec()) :: Absinthe.Middleware.spec()
def wrap(middleware_spec) do
{__MODULE__, [handle: middleware_spec]}
end
@impl true
def call(resolution, handle: middleware_spec) do
execute(middleware_spec, resolution)
rescue
error ->
Sentry.capture_exception(error, __STACKTRACE__)
Absinthe.Resolution.put_result(resolution, @default_error)
end
# Handle all the ways middleware can be defined
defp execute({{module, function}, config}, resolution) do
apply(module, function, [resolution, config])
end
defp execute({module, config}, resolution) do
apply(module, :call, [resolution, config])
end
defp execute(module, resolution) when is_atom(module) do
apply(module, :call, [resolution, @default_config])
end
defp execute(fun, resolution) when is_function(fun, 2) do
fun.(resolution, @default_config)
end
end
Applying it in Schema:
The wrap/1
method is called on all query/mutation middleware
def middleware(middleware, _field, %{identifier: type}) when type in [:query, :mutation] do
Enum.map(middleware, &ExceptionMiddleware.wrap/1)
end
Result:
Which converts them to this:
[
{ExceptionMiddleware, handle: {AuthMiddleware, [access: :admin]}},
{ExceptionMiddleware, handle: {{Resolution, :call}, &some_resolver/3}},
{ExceptionMiddleware, handle: {Subscription, []}},
{ExceptionMiddleware, handle: &anon_middleware/2},
]
I'm still not fully confident in my approach because this feels a bit hacky and a misuse of absinthe's middleware. So, I'm interested in getting answers to a couple of questions:
Absinthe.Resolution
middleware?Upvotes: 3
Views: 845
Reputation: 564
Here at Decisiv we are using an internal tool called Blunder for handling exception and errors. That might be useful for you.
https://github.com/Decisiv/blunder-absinthe
Upvotes: 0