Setting up custom response for exception in Phoenix Application

泪湿孤枕 提交于 2019-12-06 18:47:36

问题


im writing phoenix application with ecto and have the following snippet in the test

{:ok, data} = Poison.encode(%{email: "nonexisting@user.com", password: "mypass"})

conn()
|> put_req_header("content-type", "application/json")
|> put_req_header("accept", "application/json")
|> post(session_path(@endpoint, :create), data)
> json_response(:not_found) == %{}

this throws a Ecto.NoResultsError

i have this defined

defimpl Plug.Exception, for: Ecto.NoResultsError do
  def status(_exception), do: 404
end

but the test still throws Ecto.NoResultsError, Any pointers?


回答1:


Let's consider how it works per environment.

  • In :prod, the default is to render error pages, so you should see a page rendered by YourApp.ErrorView with the status code;

  • In :dev, the default is to show debug pages, because the majority of times you have an error while building your code. If you want to see the actually rendered error page, you need to set debug_errors: false in your config/dev.exs;

  • In :test, it works like production but, because you are calling your application from the test, your test will also crash if your application crashes. We are improving this on future versions, where you should be able to write something like:

    assert_raise Ecto.NoResultsError, fn ->
      get conn, "/foo"
    end
    {status, headers, body} = sent_response(conn)
    assert status == 404
    assert body =~ "oops"
    



回答2:


Phoenix 1.1.0 introduced Phoenix.ConnTest.assert_error_sent/2 to make testing similar cases easier.

From the documentation:

Asserts an error was wrapped and sent with the given status.

Useful for testing actions that you expect raise an error and have the response wrapped in an HTTP status, with content usually rendered by your MyApp.ErrorView.

Usage example:

assert_error_sent :not_found, fn ->
  get conn(), "/users/not-found"
end

response = assert_error_sent 404, fn ->
  get conn(), "/users/not-found"
end
assert {404, [_h | _t], "Page not found"} = response


来源:https://stackoverflow.com/questions/30536979/setting-up-custom-response-for-exception-in-phoenix-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!