Last active
March 3, 2017 22:21
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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