Phoenix Rendering 404 and 500 as JSON

I started a phoenix project without using the --no-html option, and now I'm trying to make sure that 404 and 500 errors are displayed as JSON. The project started on Phoenix 1.1.0 and has been upgraded to 1.1.4 .

I changed the file config/config.exs render_errors (under config :my_app, MyApp.Endpoint ) as [view: MyApp.ErrorView, format: "json", accepts: ~w(json)] .

All routes accept JSON, and currently none of them accept HTML.

I modified the web/web.ex file to remove the use of Phoenix.HTML in the view function, and I changed web/views/error_view.ex to render JSON.

However, at this point, 404 and 500 errors still return html.

+5
source share
1 answer

Have you updated your config.exs ?

 config :my_app, MyApp.Endpoint, # ... render_errors: [accepts: ~w(html json)], # ... 

And what html is returning? Perhaps this is the phoenix debug page for your development environment, which you can turn off in config/dev.exs

 config :my_app, MyApp.Endpoint, # ... debug_errors: false, # ... 
+5
source

Source: https://habr.com/ru/post/1246076/


All Articles