Phoenix渲染404和500为JSON [英] Phoenix Rendering 404 and 500 as JSON

查看:81
本文介绍了Phoenix渲染404和500为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不使用--no-html选项的情况下启动了一个phoenix项目,现在正在尝试确保404500错误呈现为JSON.该项目从Phoenix 1.1.0开始,现已更新为1.1.4.

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.

我已经将config/config.exs文件的render_errors(在config :my_app, MyApp.Endpoint下)修改为[view: MyApp.ErrorView, format: "json", accepts: ~w(json)].

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)].

所有路由均接受JSON,目前均不接受HTML.

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

我已经修改了web/web.ex文件以删除view函数中的Phoenix.HTML使用,并且我已经修改了web/views/error_view.ex来呈现JSON.

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.

但是,此时404500错误仍然返回html.

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

推荐答案

您是否更新了config.exs?

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

又返回哪种html?也许这是您的开发环境的phoenix调试页面,您可以在config/dev.exs

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,
  # ...

这篇关于Phoenix渲染404和500为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆