dimitarvp

dimitarvp

Plug.ErrorHandler: how to use to return JSON errors?

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?

Most Liked

dimitarvp

dimitarvp

Nope, that didn’t work. :expressionless:

Hitting a non-existent endpoint with curl:

HTTP/1.1 404 Not Found
cache-control: max-age=0, private, must-revalidate
content-length: 1333
content-type: text/markdown; charset=utf-8
date: Mon, 12 Apr 2021 16:33:02 GMT
server: Cowboy

# Phoenix.Router.NoRouteError at POST /does_not_exist

Exception:

    ** (Phoenix.Router.NoRouteError) no route found for POST /does_not_exist (MyAppWeb.Router)
# <snip>

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

Exadra37

Sorry for not having given you a good solution :frowning:

But did you try the Error view approach? I think that one should work :thinking:

Because you are in development mode. Go to config/dev.exs and set debug_errors to false.

Last Post!

Exadra37

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/2 function 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/2 to return the Json error.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
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

We're in Beta

About us Mission Statement