I'm trying to extend an existing exception in Phoenix.
I've seen from the docs to do this:
defimpl Plug.Exception, for: Ecto.NotSingleResult do
def status(_exception), do: 404
end
But where does this go?
Also, I have two pipelines, one for :browser, one for :api Is it possible to extend in one pipeline and not the other?
You can put the defimpl
for the Plug.Exception
protocol wherever you want, as long as the file it's in is loaded by Mix (e.g., files in lib
or web
). For example, you can create lib/my_app/plug_exception_implementations.ex
with this content:
defimpl Plug.Exception, for: Ecto.NotSingleResult do
def status(_exception), do: 404
end
# other `defimpl Plug.Exception`s here if needed
Once you define the implementation of a protocol for a data type, it's global so it would be defined for both pipelines.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With