jsonapi icon indicating copy to clipboard operation
jsonapi copied to clipboard

Sending properly formatted 404 Not Found

Open strzibny opened this issue 6 years ago • 3 comments

Hello, first thanks for this library, looking great.

I am trying to test 404 endpoints and realized that jsonapi maybe does not catch Ecto.NoResultsError and does not show proper 404 error.

It should show a JSON:API spec formatted error as for any other:

{"errors":[{"status":"404","title":"Page not found"}]}

am I not realizing something? It feels like it counts on Phoenix to serve 404 error which is without the formatted message above.

strzibny avatar Mar 14 '19 22:03 strzibny

Interesting. In a project I have I see the following:

{"errors":[{"status":"404","title":"Page not found"}]}

I have a bit of manual error set up though. I'm sure we can do something to improve this situation.

jherdman avatar Mar 15 '19 15:03 jherdman

Just for anybody coming here I can achieve this with the following:

defmodule Web.ErrorView do
  use Web, :view

  # 404 Not Found following JSON:API
  def render("404.json", _assigns) do
    Jason.encode!(%{errors: [%{status: "401", title: "Not Found"}]})
  end
end

It works, but won't allow me to easily test this in a controller test, because I need to catch the exception. Maybe jsonapi could catch the exception before Phoenix does and this way it would work? I am sorry I am just one-week-in in Phoenix so a bit hard for me to figure this out on my own.

strzibny avatar Mar 15 '19 22:03 strzibny