Better Low-Level Error Handling with Puma

in Puma , Ruby

Puma has a neat little method you can call to override the behavior when it encounters an error in the Rack application it’s running. If you’ve ever been running a Ruby application on Puma and seen the “call your local MayTag repairman” error message then you’ve hit the low-level error handler and know how jarring that can be. While it looks like the folks at Puma have since updated the default message to be more meaningful, I still wanted something more customer-friendly for our sites at Engage so I overwrote the low-level error handler like so:

1# Inside of your Puma configuration file.
2lowlevel_error_handler do
3  [500, { 'Content-Type' => 'text/html' }, File.open('50x.html')]
4end

And added a little more helpful HTML page with it:

 1<!doctype html>
 2<html>
 3  <head>
 4    <meta charset='utf-8'>
 5    <meta http-equiv='refresh' content='5'>
 6    <title>Temporarily Unavailable</title>
 7    <link rel='stylesheet' type='text/css' href='//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css'>
 8  </head>
 9  <body>
10    <div class='container'>
11      <div class='col-sm-offset-3 col-sm-6 text-center'>
12        <br>
13        <br>
14        <h1>Temporarily Unavailable</h1>
15        <br>
16        <div class='well'>
17          <p>We're sorry, we're experiencing a little trouble.</p>
18          <p>We will be back shortly, but if you continue to see this error please <a href='mailto:service@cardserviceteam.com'>contact customer service</a>.</p>
19          <hr>
20          <p>Feel free to realod this page or <a href='/'>head back to the homepage</a>.</p>
21        </div>
22      </div>
23    </div>
24  </body>
25</html>

Super simple, right?