I’m having some trouble getting a simple example to work. I’m not using Phoenix FWIW, just plug
defmodule Unauthorized do
defexception message: "not authorized", plug_status: 401
end
defmodule Foo do
use Plug.Router
plug :match
plug :dispatch
get "/" do
send_resp(conn, 200, "heyyyo")
end
get "/boom" do
raise Unauthorized
end
match _ do
send_resp(conn, 404, "not found")
end
end
This is kind of a silly example, but I’m just trying to see if it will work like I think it is supposed to work.
I was hoping that Plug would handle the exception raised in GET /boom
and return a 401 status
However, when I do try to GET /boom
it is returning a 500 status, so apparently the exception isn’t being handled by Plug
You need to add use Plug.ErrorHandler
if you want to catch these exceptions and send an HTTP status code based on the exception that was raised:
defmodule Foo do
use Plug.Router
use Plug.ErrorHandler
...
end
With this change, I get the correct response:
$ curl -i http://localhost:4000/boom
HTTP/1.1 401 Unauthorized
server: Cowboy
date: Wed, 17 May 2017 19:59:57 GMT
content-length: 20
cache-control: max-age=0, private, must-revalidate
Something went wrong