Handling Plug.Parsers.ParseError in a Phoenix API

I am implementing a JSON API using Phoenix, and one of the requirements is to return a custom JSON error message when a client sends malformed JSON. After looking at the docs for Phoenix and Plug, I still don’t know how to do this.

So when sending something like ‘{“hello”: "world}’ to the API, it should return something like ‘{“errcode”: “M_NOT_JSON”}’ with status code 400. Right now, it only logs the Plug.Parsers.ParseError and render HTML

Did you add anything in your view file?

I’m not getting much idea what is there in your view file.
I have tried to mention here how can you build a simple api. Maybe this could help?

I’m not using any views. I am responding with json with

conn
|> put_status(200)
|> json(payload)

I would like to do the same when Plug isn’t able to decode input JSON.