mszmurlo

mszmurlo

How to use Plug.ErrorHandler to handle errors

Hi.

I’m working on a webapp based only on bandit and plugs only and I’m wondering how to handle errors.

For 404 I can put something like

  match _ do
    ErrorController.error_404(conn)
  end

at the end of the routes list, but that doesn’t catch all other error, as for example 500. This morning I saw there was a dedicated plug to handle error, yet the documentation is pretty short… And since then I’m struggling to understand how to use it.

My router looks like this:

defmodule MEC.Web.Router do

  @moduledoc false
  use Plug.Router
  use Plug.ErrorHandler

  
  require Logger
  alias MEC.Web.PageController
  alias MEC.Web.ErrorController
  
  plug Plug.Logger
  plug :match
  plug :dispatch
  
  get "/", do: PageController.index(conn)
  get "/test", do: FakeController.index(conn)  # NOT DEFINED. To simulate an error 500
  
##   match _ do
##     ErrorController.error_404(conn)
##   end
  
  defp handle_errors(conn, %{kind: kind, reason: reason, stack: _stack}) do
    resp = """
Something went wrong:
  kind=#{inspect(kind)}
  reason=#{inspect(reason)}
  conn=#{inspect(conn, pretty: true)}
"""
    send_resp(conn, conn.status, resp)
  end
  
end

Notice that match _ do is commented to check how ErrorHandler handles 404 errors and the controller FakeController is not defined at all to simulate an error 500.

  • for /: as it is not an error, all is fine
  • for /test : I get an error 500 (only seen in conn.status) which is OK
  • for /other which sould be an error 404, I get a 500 with the reason being reason=%FunctionClauseError{module: MEC.Web.Router, function: :do_match, arity: 4, kind: nil, args: nil, clauses: nil}

This is where I’m stuck. Any hint is welcome

First Post!

Hermanverschooten

Hermanverschooten

Plug.Router does not handle the 404 by default according to the docs.

This also means that a catch all match block is recommended to be defined as in the example above, otherwise routing fails with a function clause error (as it would in any regular Elixir function).

And this is exactly what you are seeing.

Where Next?

Popular in Questions Top

aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement