Skip to content

Instantly share code, notes, and snippets.

@preaction
Last active March 3, 2017 22:21
Show Gist options
  • Save preaction/527f9ea311da9ebf8ec5cc7739af49b8 to your computer and use it in GitHub Desktop.
Save preaction/527f9ea311da9ebf8ec5cc7739af49b8 to your computer and use it in GitHub Desktop.
Render a useful JSON API error message including the exception, a stack trace, and a log
use Mojolicious::Lite;
use Log::Any qw( $LOG );
use Log::Any::Adapter;
Log::Any::Adapter->set( 'MojoLog', logger => app->log );
app->log->max_history_size( 100 );
get '/' => sub { $LOG->info( "LOG::ANY IS AMAZING" ); die "This should di... ALL GLORY TO THE HYPNOTOAD!\n" };
app->start;
__DATA__
@@ exception.html.ep
% use Mojo::JSON qw( to_json );
% $c->res->headers->content_type( 'application/json' );
{
"errors": [
{
"message": <%== to_json stash( 'exception' )->message %>,
"stack_trace": <%== to_json stash( 'exception' )->frames %>
}
],
"log": <%== to_json app->log->history %>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment