Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eprothro/b0d1a1c50cc9287971945dc1f22772ad to your computer and use it in GitHub Desktop.
Save eprothro/b0d1a1c50cc9287971945dc1f22772ad to your computer and use it in GitHub Desktop.
defmodule MyGraph.Middlewares.ResolutionWithErrorReporting do
@moduledoc """
Middleware to handle application errors and exceptions
"""
require Logger
@behaviour Absinthe.Middleware
@doc """
Call this on existing middleware to replace instances of
`Resolution` middleware with `ResolutionWithErrorReporting`
"""
@spec apply(list()) :: list()
def apply(middleware) when is_list(middleware) do
Enum.map(middleware, fn
{{Absinthe.Resolution, :call}, resolver} -> {__MODULE__, resolver}
other -> other
end)
end
@impl true
def call(resolution, resolver) do
Absinthe.Resolution.call(resolution, resolver)
rescue
exception ->
_ = Sentry.capture_exception(exception)
# If our graph schema didn't handle the error
# we want an HTTP 500, not a silent failure
# e.g. with a graph result including an error like
# Absinthe.Resolution.put_result(resolution, {:error, :unknown})
reraise exception, __STACKTRACE__
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment