How to Display Plug Errors

This post is aimed at those who are just starting to use Plug, a great web middleware for Elixir language.

TL;DR

If you want to access various error messages from Plug that do not show up in Logger, Plug.Debugger will make those failed requests appear. Check out Plug.Debugger.

404 Error Message Not Found

While building an app that received JSON through HTTP POST requests, I noticed an interesting behavior. When I send a request with malformed JSON payload, the request would magically banish without any trace in either the log or the server.

So when I send a request with malformed JSON like:

The server would respond with 500 Internal Server Error without displaying or logging internal error message.

After some digging, I found out that the sort of error I was looking for - Plug.Parsers.ParseError - is not logged by default. That makes sense - I don’t want my log to be flooded with error messages about potentially unlimited number of bad requests. But what if I actually wanted to check the error message?

The solution is to use Plug.Debugger in the module that uses Plug.Router.

The following is part of the code for the web interface of my app. Notice the use Plug.Debugger I included here.

After calling that macro, your server now internally logs the error message.

It also responds to the HTTP request with an error message, so that could be another way for debugging your app.