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.
Trending in Questions
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
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
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 14 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
Phoenix does a bit more, but conceptionally it works exactly the same as
Plug.ErrorHandler. It also reraises errors: phoenix/lib/phoenix/endpoint/render_errors.ex at 246ca7726e14ed94d3d878838cbd6add5a0df3e9 · phoenixframework/phoenix · GitHubadamu
My understanding (albeit potentially flawed) is that there are three methods for handling errors, and two places for defining the error response in Phoenix:
:errortuple in the controller and use the fallback controller, as @D4no0 mentioned. Unfortunately, this is not helpful for you, as you’re dealing with a raisedEcto.NoResultsError.Plug.ErrorHander, as @tfwright mentioned. However, I believe that this is more designed for handling side effects, as the error is reraised to be handled by point 3 below.LostKobrakai
How did you try that? Because the http request should receive whatever you send as response in the handler. It shouldn’t matter that the error is reraised (e.g. to get properly logged).
See Phoenix.ConnTest — Phoenix v1.7.7 for testing those.
While the latter is a viable option the former is not generally true. Exceptions are the only way to handle errors from LV right now (via Plug.ErrorHandler) and phoenix itself for a long time didn’t have
action_fallbackwhile already having means of handling exceptions.D4no0
Let me reformulate this so it is crystal clear. “Using exceptions to handle edge cases is not the way to go”.
As for minority, let’s stay educated and not generalise based on some subjective experience.
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.ErrorHandlerfor 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).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
withstatement 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:
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).
whoiswentz
I have a huge code base that some functions raise errors, I didn’t have a choice
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.
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…