Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
more complete exception handling example
  • Loading branch information
kraih committed Sep 10, 2014
1 parent a4b9e1a commit cad17b9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -525,12 +525,34 @@ loop, outside of the application, errors in callbacks can't get caught and
logged automatically, you can change that by subscribing to the event
L<Mojo::Reactor/"error">.

use Mojolicious::Lite;
use Mojo::IOLoop;

# Forward error messages to the application log
Mojo::IOLoop->singleton->reactor->on(error => sub {
my ($reactor, $err) = @_;
app->log->error($err);
});

# Exception only gets logged (and connection times out)
get '/connection_times_out' => sub {
my $c = shift;
Mojo::IOLoop->timer(2 => sub {
die 'This request will not be getting a response';
});
};

# Exception gets caught and handled
get '/catch_exception' => sub {
my $c = shift;
Mojo::IOLoop->timer(2 => sub {
eval { die 'This request will be getting a response' };
$c->render_exception($@) if $@;
});
};

app->start;

A default subscriber that prints all errors to C<STDERR> will usually be added
by L<Mojo::IOLoop> as a fallback.

Expand Down

0 comments on commit cad17b9

Please sign in to comment.