I showed it to you in my first post:
Exadra37:
An Alternative that you can try is to change the Error view to always return a JSON object:
defmodule YourAppWeb.ErrorView do
use YourAppWeb, :view
require Logger
# If you want to customize a particular status code
# for a certain format, you may uncomment below.
def render(template, assigns) do
_json_error(template, assigns)
end
# By default, Phoenix returns the status message from
# the template name. For example, "404.html" becomes
# "Not Found".
def template_not_found(template, assigns) do
_json_error(template, assigns)
end
defp _json_error(template, assigns) do
Logger.info("Error template: #{template}")
# render("500.html", assigns)
# I am not sure, but I think you can just return a Map, otherwise just try to use the Jason library.
%{
error: 400, # You need to extract it from assigns or template name
maessage: "Whoops!"
}
end
end
This file can be found in your app web views folder.
Basically ti’s overriding the render/2 function called to render the error templates for each error status code that is defined in the templates folder.
It also customizes the template_not_found/2 to return the Json error.