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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews