In my Api server project I had these exception from ecto like Ecto.NoResultsError. is there a way for me to catch this exception so i can return as json format instead of html? since it will be more DRY of using the bang method rather than written condition in everything where i use the Ecto method.
If you are using plug, you might be interested in Phoenix.NotAcceptableError - How can I return an error instead of having the app just have an exception?
@lastday4you You want to use Plug.Exception for this. In fact, Phoenix already has a Plug.Exception for Ecto.NoResultsError which converts the exception to a status code 404. This presents a simple 404 page in production. (yes, you can use the bang method by default for Ecto repo calls). You can create custom Plug.Exception modules for any Exception you want to generically catch.
Since you’re creating an API server you can make good use of
action_fallback if you switch away fro the bang version of the Ecto methods.