Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phoenix Rendering 404 and 500 as JSON

I started a phoenix project without using the --no-html option, and am now trying to ensure that the 404 and 500 errors render as JSON. The project started on Phoenix 1.1.0, and has been updated to 1.1.4.

I've modified the config/config.exs file's render_errors (under config :my_app, MyApp.Endpoint) to be [view: MyApp.ErrorView, format: "json", accepts: ~w(json)].

The routes all accept JSON and currently none of them accept HTML.

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

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

like image 809
brittonjb Avatar asked Mar 30 '16 12:03

brittonjb


1 Answers

Did you update your config.exs?

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

And what kind of html is returned? Maybe it is the phoenix debug page for your dev environment which you can disable in config/dev.exs

config :my_app, MyApp.Endpoint,
  # ...
  debug_errors: false,
  # ...
like image 193
Ole Spaarmann Avatar answered Nov 15 '22 13:11

Ole Spaarmann