whoiswentz

whoiswentz

Sup guys.

I’m running into a problem and doubt, I need to create a Plug that runs after the Router, basically, I have a controller that throws an exception eg. Ecto.NoResultsError, I need to catch this error using a plug, handle it and return a JSON to the client.

Showing Posts 1 to 10

tfwright

tfwright

Are you using Phoenix? My memory was that this works out of the box.

But maybe take a look at Plug.ErrorHandler — Plug v1.20.2

whoiswentz

whoiswentz OP

Yes, I’m using Phoenix

I already tried the Plug.ErrorHandler, but it re-raises the error as the documentation points

I need a solution to enrich the error message and return a JSON

tfwright

tfwright

Why is the error reraising is undesirable for your use case? Your user is still going to get a json response (and the handlers allow that to be anything).

D4no0

D4no0

Raising an error is not the way you do things in elixir.

Instead of raising, you can return a tuple in the format {:error, :not_found} and handle it in a fallback controller.

tfwright

tfwright

Curious what you mean by this. Certainly runtime errors, and therefore error raising, is a feature of the language? Or maybe you just mean that raising an error in this case is a bad pattern? But Plug literally has built in support for this and Phoenix is configured to take advantage of it by default, so while opinions may differ it is certainly “the way you do things” for many people…

D4no0

D4no0

It’s a feature to pass errors up to the parent process, why you would use it in this case is beyond my understanding.

whoiswentz

whoiswentz OP

I have a huge code base that some functions raise errors, I didn’t have a choice :frowning:

tfwright

tfwright

Convenience I think would be the main reason. I assume your problem with Plug’s design is that generally it is a bad idea to use errors to control flow. But since we generally want to respond with a user friendly error even when an exception is not expected, this kind of logic seems required here. And once it’s necessary it seems too convenient not to take advantage of in cases where the exception is not strictly speaking unexpected but does not and could not require any special logic other than building the response, because the alternative is needing separate and explicit handling for every single API requirement (like parameter shape/format).

D4no0

D4no0

My problem is in general related to using exceptions as flow, this is the same as using goto statements, error prone and totally unreadable.

You don’t want to respond to a user with a specific response when something unexpected happens, as this is a bug in the system, and the user doesn’t need to know the details, as you are potentially exposing a exploitation interface. In the case with not found, that is not a bug though, it is a case that should be handled appropriately and using a fallback controller in conjunction with a with statement is the most appropriate solution.

I don’t see how this is the case here, you just handle the error response coming from your controller:

def call(conn, {:error, :not_found}) do
    conn
    |> put_status(:not_found)
    |> put_view(MyErrorView)
    |> render(:"404")
  end
tfwright

tfwright

I didn’t intend to make this a debate about the merits of the approach. Again, I just wanted to point out that, unless I am greatly mistaken, your claim that using Plug.ErrorHandler for this is not “the Elixir way” is almost exactly opposite of the truth, it’s actually the default in Phoenix which I would say makes the most common approach. I don’t think it’s helpful to formulate minority opinions as if they are majority (which of course is not to say that your opinion must be wrong because it’s the minority).

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews