-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Unclear how to handle errors in Phoenix.Endpoint #2791
Comments
Phoenix' call is happening in a Also note that you shouldn't replicate what Phoenix does, because you would be fiddling with Phoenix internals and that's error prone. :) Although you probably only did that for experimentation purposes. |
Here is the building block that you want: defmodule Sample do
defmacro __using__(_opts) do
quote do
@before_compile Sample
end
end
defmacro __before_compile__(_) do
quote do
defoverridable call: 2
def call(conn, opts) do
try do
super(conn, opts)
catch
kind, reason ->
Sentry.stuff(...)
:erlang.raise(kind, reason, System.stacktrace)
end
end
end
end
end It should work for plugs, phoenix, etc. |
@josevalim thanks for the quick response! that works perfectly 💖 |
@josevalim Can we use the same to tackle this problem? absinthe-graphql/absinthe#1219 |
Environment
Apologies ahead of time as this is an issue that is difficult for me to explain, so this is a bit rambly 🙂
This originated from getsentry/sentry-elixir#229
Currently, to catch errors in Phoenix during the request process, using Plug.ErrorHandler in the Router works well. The limitation being if the error happens before the request reaches the Router in the Endpoint, and I haven't been able to find a way I like to catch them.
Since
Phoenix.Endpoint
defines an overridablecall/2
here I was thinking I could override and include my own error handling code by adding something like:This works in that my error handling service gets called. The issue with it is it seems like both the original
call/2
that Phoenix.Endpoint defines gets called first, and then mine gets called. It seems that way because the inspect above prints out:private
with the endpoint module before my code puts it in there.I also tried to
use Plug.ErrorHandler
in the Endpoint, but anyhandle_errors/2
that get defined don't get called. I'm guessing becausePhoenix.Endpoint
works similarly in defining an overridablecall/2
function?Expected behavior
I'm not sure how I would expect to be able to handle the errors. I'm happy to try solutions I have missed or put together a PR.
If there isn't a good way currently, being able to define a function to handle errors in the
Phoenix.Endpoint
catch could work (similar toPlug.ErrorHandler
), but I'm open to anything that makes it easier 🙂The text was updated successfully, but these errors were encountered: