xgeek116

xgeek116

Phoenix do not take into consideration my custom errorView

I have defined a custom exception :


  defmodule UnkownBookError do
    @moduledoc false
    defexception message: "Unknown book id", plug_status: 404
  end

In ErrorView module I have added my custom error view func :

  def render("error.json", %{message: message}) do
    %{errors: %{detail: message}}
  end

But I always get this in the console :

[debug] Converted error Booking.BookHandler.UnkownBookError to 404 response

and I get a text response in Postamn instead of JSON :

What I’m missing here ?

Thanks

Marked As Solved

fuelen

fuelen

  1. What headers do you have? Have you sent Accept: application/json?
  2. Matching by template name is invalid. In case of 404, the template is the following: 404.json, not error.json.
  3. The second parameter is not an exception, but assigns. The exception could be found in assigns.reason.

If you want generic handler for JSON format:

def render(<<_::binary-size(3)>> <> ".json" = template, assigns) do
  %{
    error: Phoenix.Controller.status_message_from_template(template),
    description: Exception.message(assigns.reason)
  }
end

but don’t show a message from all exceptions, as they may contain not public information

Also Liked

xgeek116

xgeek116

Thanks a lot @fuelen , so I get that “error_view.ex” is responsible from rendring theese errors, so what is the purpose of “fallback_controller.ex” ?

For example in my file “fallback_controller.ex” I have only :

I’m not defining how Forbidden (403 status) will be handled, and If I try a forbidden action I get the correct error :

LostKobrakai

LostKobrakai

action_fallback is about consolidating common error return values (of your business logic) and how they’re transformed to http responses (it’s about data).

MyAppWeb.ErrorView is a view, so it’s about transforming elixir data into json/html/whatever format your http response should be formatted in. This one specifically is about rendering responses for the various http error codes.

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
New

We're in Beta

About us Mission Statement