Sheharyar
Sheharyar

Reputation: 75840

Handling Exceptions in Absinthe

The Issue

There are a lot of guides available for handling error tuples in 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 response/error spec. This can be especially problematic when GraphQL clients like automatically batch requests, and an exception in one query will crash the whole BEAM web process causing all queries to fail.


Existing Approaches

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:


My Solution

My 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},
]

Question(s)

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:

Upvotes: 3

Views: 845

Answers (1)

Marcos Tapajós
Marcos Tapajós

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

Related Questions