diff --git a/lib/Dancer2/Plugin/Ajax.pm b/lib/Dancer2/Plugin/Ajax.pm index c6b7c62..1321b7b 100644 --- a/lib/Dancer2/Plugin/Ajax.pm +++ b/lib/Dancer2/Plugin/Ajax.pm @@ -43,8 +43,14 @@ sub ajax { # disable layout my $layout = $plugin->app->config->{'layout'}; $plugin->app->config->{'layout'} = undef; - my $response = $code->(); + my $response = eval { $code->(); }; + my $error = $@; + # Restore layout first $plugin->app->config->{'layout'} = $layout; + # Then give way to the route exception + if( $error ) { + die($error); + } return $response; };