dimitarvp
I’m looking into returning JSON errors from an API app. I know the specs dictate you must always return HTML on certain error codes but I want to circumvent that because I’ll have complete control both on the calling side and the service itself. Plus the service will send logs and telemetry and I want errors to be machine-parseable.
I tried a few things after searching the forum but I couldn’t manage to make any of them work.
Then I stumbled upon Plug.ErrorHandler — Plug v1.20.2. Call me stupid but I have no clue how to use it?
So as a start I want all 404s and 500s to be wrapped with a JSON key errors in an object, returned alongside a 404 and 500 status.
Is this possible using Plug.ErrorHandler? If so, how exactly?
Trending in Questions
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
Hi all!
I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
I have a page in my Phoenix app with some LiveView code. When you click a button, it shows the Stripe credit card modal form via a Hook. ...
New
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
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
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hi everyone :waving_hand:
I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #security
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Exadra37
Please bear in mind that all what I am about to suggest is from the top of my mind, therefore not tested, and may be wrong, but hey it doesn’t hurt for you to give it a try
If I am not mistake the above will override the call to the Error view at
lib/yourapp_web/views/error_view.ex.I think that this will return it in json:
Maybe you can simplify the above to use in your router:
EDIT: On a second though I think this isn’t reflecting the intended use from the Plug.ErrorHandler
An Alternative that you can try is to change the Error view to always return a JSON object:
dimitarvp
I still don’t understand where to put this copy-pasted module code in my Phoenix project, and how do I register it to be used – in the router, the endpoint, somewhere else?
The docs kind of assume certain knowledge that I found I don’t have.
Exadra37
I update my previous post while you made this one to make it clear, but here it his again:
This is a guess from me, not actually tried it.
dimitarvp
Nope, that didn’t work.
Hitting a non-existent endpoint with
curl:And it’s returning markdown text? Why?
I am still trying to understand how do I just intercept ALL errors and wrap them in keys in a resulting JSON object.
Exadra37
Sorry for not having given you a good solution
But did you try the Error view approach? I think that one should work
Because you are in development mode. Go to
config/dev.exsand setdebug_errorsto false.dimitarvp
No idea what to do exactly there either.
It’s been an absolutely awful day and I’d love complete code examples. At least today. If not, I’ll try formulating a solution on a fresh head tomorrow.
Exadra37
I showed it to you in my first post:
This file can be found in your app web views folder.
Basically ti’s overriding the
render/2function called to render the error templates for each error status code that is defined in the templates folder.It also customizes the
template_not_found/2to return the Json error.