Scoped exception handling in Phoenix

Hi there,

I am currently working on a Phoenix project that will provide several different APIs to users. Some of them use JSON-RPC and I would like to be able to give a JSON-RPC compliant error message when the parsing of the JSON sent by a client has failed.

Now, Phoenix is configured by default to automatically parse client-sent JSON early on through Plug.Parsers. When an exception occurs there, it gets handled by the Endpoint.
Depending on which version of the API a user is accessing, I would like to send different error responses.

Is there any way to intercept exceptions in the Plug pipeline at the controller or Router scope level instead?
I have not found a way to do so; am I maybe missing something? My approach to allow the behavior I want would otherwise be to remove JSON parsing from the Endpoint and move it to the individual controllers or pipelines so I can intercept any exceptions directly from there.

Thank you for your suggestions.


That’s the way to go!