How to capture an unexpected error in phoenix controller? => returning json not working

How do you create a handler to return a error template for a unexpected error in phoenix? Calling the following controller don’t result in any results. Which results in the default handler not being called. I would expect this to be called but it’s not. The result is a 500 with no content being returned.

  def index(conn, _params) do
    # TODO: find out how to handle random errors
    raise ArgumentError, message: "invalid argument foo"
    conn
    |> put_resp_content_type(@txt_content_type)
    |> send_resp(200, @static_text_data)
  end


# file:error_view.ex
  def template_not_found(template, _assigns) do
    %{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}, template: template, other: Phoenix.Controller.status_message_from_template("wow")}
  end



          (plug) lib/plug/conn.ex:536: Plug.Conn.send_resp/3
        (phoenix) lib/phoenix/endpoint/render_errors.ex:60: Phoenix.Endpoint.RenderErrors.__catch__/5
        (plug) lib/plug/adapters/cowboy/handler.ex:16: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) /Users/smorin/code/phx_app/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

smorin-local:phx_app smorin$ curl -i --request GET ‘http://127.0.0.1:4000/basic/color.json
HTTP/1.1 500 Internal Server Error
server: Cowboy
date: Fri, 06 Jul 2018 00:52:29 GMT
content-length: 0

#phoenix

2 Likes

:wave:

Maybe give https://hexdocs.pm/plug/Plug.ErrorHandler.html a try.

Thanks just found that.