Thomas Klausner


Plack::Middleware::PrettyException - Capture exceptions and present them as HTML or JSON


version 1.002


  use Plack::Builder;
  builder {
      enable "Plack::Middleware::PrettyException",

  # then in your app in some controller / model / wherever

  # just die
  die "something went wrong";

  # use HTTP::Throwable
      NotAcceptable => { message => 'You have to be kidding me!' }

  # use a custom exception that implements http_status
  My::X::BadParam->throw({ status=>400, message=>'required param missing'})

  # clients get either
  #   JSON (if Accept-header indicates that JSON is expected)
  #   or a plain HTML error message


Plack::Middleware::PrettyException allows you to use exceptions in your models (and also controllers, but they should be kept slim!) and have them rendered as JSON or nice-ish HTML with very little fuzz.

But if your Plack app returns an HTTP status code indicating an error (4xx/5xx) or just dies somewhere, the client also sees a pretty exception.

So instead of capturing exceptions in your controller actions and converting them to proper error messages, you just let the exception propagate up to this middleware, which will then do the rendering. This leads to much cleaner code in your controller, and to proper exception usage in your model.


Here is am example controller implementing some kind of update:

  sub some_update_action {
      my ($self, $req, $id) = @_;

      my $item = $self->some_model->load_item($id);
      my $user = $req->get_user;
      $self->auth_model->may_edit($user, $item);

      my $payload = $req->get_json_payload;

      my $rv = $self->some_model->update_item($item, $payload);


The lack of error handling makes the intention of this piece of code very clear. "The code is easy to reason about", as the current saying goes.

Here's the matching model

  # SomeModel
  sub load_item {
      my ($self, $id) = @_;

      my $item = $self->resultset('Foo')->find($id);
      return $item if $item;

          message=>'Cannot load Foo from id %{id}s',

My::X::NotFound could be a exception class based on Throwable::X:

  package My::X;
  use Moose;
  with qw(Throwable::X);
  use Throwable::X -all;
  has [qw(http_status)] => (
      is      => 'ro',
      default => 400,
      traits  => [Payload],
  no Moose;
  package My::X::NotFound;
  use Moose;
  extends 'My::X';
  use Throwable::X -all;
  has id => (
      is     => 'ro',
      traits => [Payload],
  has '+http_status' => ( default => 404, );

If we now call the endpoint with an invalid id, we get:

  ~$ curl -i http://localhost/thing/42/update
  HTTP/1.1 404 Not Found
  Content-Type: text/html;charset=utf-8
    <head><title>Error 404</title></head>
      <h1>Error 404</h1>
      <p>Cannot load Foo from id 42</p>

If we want JSON, we just need to tell the server:

  ~$ curl -i -H 'Accept: application/json' http://localhost/thing/42/update

  HTTP/1.1 404 Not Found
  Content-Type: application/json

  {"status":"error","message":"Cannot load Foo from id 42"}


Content Negotiation / Force JSON

As of now there is no real content-negotiation, because all I need is HTML and JSON. There is some semi-dumb checking of the Accept-Header, but I only check for literal application/json (while I should do the whole q-factor weighting dance).

If you want to force all your errors to JSON, pass force_json = 1> when loading the middleware:

  builder {
      enable "Plack::Middleware::PrettyException" => ( force_json => 1 );

This will be replace in the near future by some proper content negitiation and a new default_response_encoding field.

Finetune HTML output via subclassing

The default HTML is rather basic^wugly. To finetune this, just subclass Plack::Middleware::PrettyException and implement a method called render_html_error. This method will be called with the HTTP status code and the error message, and you can then render it as fancy as you (or your graphic designer) wants.

Here's an example:

  package Oel::Middleware::Error;
  use 5.020;
  use strict;
  use warnings;
  use parent qw(Plack::Middleware::PrettyException);
  use Plack::Util::Accessor qw(html_page_model renderer);
  sub render_html_error {
      my ( $self, $status, $message ) = @_;
      my %data = (base=>'/',title=>'Error '.$status, error => $message, code => $status );
      eval {
          if (my $page = $self->html_page_model->load('/_error/'.$status)) {
            $data{title} = $page->title;
            $data{description} = $page->teaser;
      my $rendered='';
      return $rendered if $rendered;
      return "Error while rendering error: ".$self->renderer->tt->error;

This middleware uses a html_page_model to retrieve the title and description of the error page from a database (where admins can edit those fields via a CMS). It uses Template::Toolkit to render the page.

html_page_model and renderer are two attributes needed by this middleware, implemented via Plack::Util::Accessor. You have to provide some meaningful objects when loading the middleware, maybe like this:

  use Plack::Builder;

  builder {
      enable "Plack::Middleware::PrettyException",
          renderer        => Oel::Renderer->new,
          html_page_model => Oel::Model::HtmlPage->new;


Of course you'll need to init Oel::Renderer and Oel::Model::HtmlPage, so you'll probably want to use Bread::Board or OX.



Thanks to

  • for supporting Open Source.

  • for the motivation to extract the code from the Validad stack.

  • sixtease for coming up with Oel as the name for my example app (after miss-reading oe1).


Thomas Klausner <>


This software is copyright (c) 2016 by Thomas Klausner.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.