Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phoenix where to defimpl Plug.Exception

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?

like image 958
Josh Petitt Avatar asked Feb 09 '23 09:02

Josh Petitt


1 Answers

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.

like image 167
whatyouhide Avatar answered Feb 19 '23 10:02

whatyouhide