Last active
September 14, 2022 16:32
-
-
Save eprothro/b0d1a1c50cc9287971945dc1f22772ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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