500 Internal Server Error in Dancer
Mistakes can happen. There might be an exception somewhere in one of the routes. Don't worry though. If that happens Dancer will show a standard "500 Internal Error" page.
In our sample application the "/calc" route tries to make some calculation but a division by 0 error occures. This will trigger the "500 Internal Error".
Usually you don't plan to have certain URLs and certain input generate such error, so you probably will never write a test for this, but now, that we are showing it I put together one.
Later we'll see how can we change the content of this page to be branded to our site.
package App;
use Dancer2;
get '/' => sub {
return 'Hello World!';
};
get '/calc' => sub {
my $x = 1;
my $y = 0;
my $z = $x / $y;
return 'OK';
};
App->to_app;
use strict;
use warnings;
use Test::More;
use Plack::Test;
use Plack::Util;
use HTTP::Request::Common;
my $app = Plack::Util::load_psgi './app.psgi';
my $test = Plack::Test->create($app);
subtest main => sub {
my $res = $test->request(GET '/');
is $res->status_line, '200 OK', 'Status';
is $res->content, 'Hello World!', 'Content';
};
subtest calc => sub {
my $res = $test->request(GET '/calc');
is $res->status_line, '500 Internal Server Error', 'Status';
like $res->content, qr{<title>Error 500 - Internal Server Error</title>};
like $res->content, qr{Powered by <a href="http://perldancer.org/">Dancer2</a>};
};
done_testing();