I’ve finished to polish the error catcher I was working on and thought it was a good idea to push it to CPAN. I did so a minute ago, expect it to be indexed by CPAN within the next hour.
The major feature of this new release is the error catching, let’s take a glance at that and see how Dancer now handles the nasty web errors we are so afraid of:
Let’s start by creating a new application from scracth, that’s very easy now thanks to the helper:
$ dancer -a NightClub
+ NightClub
+ NightClub/views
+ NightClub/views/index.tt
+ NightClub/views/layouts
+ NightClub/views/layouts/main.tt
+ NightClub/environments
+ NightClub/environments/development.yml
+ NightClub/environments/production.yml
+ NightClub/config.yml
+ NightClub/app.psgi
+ NightClub/public
+ NightClub/public/css
+ NightClub/public/css/style.css
+ NightClub/public/css/error.css
+ NightClub/public/images
+ NightClub/public/404.html
+ NightClub/public/500.html
+ NightClub/NightClub.pm
+ NightClub/NightClub.pl
The NightClub application is now created and is already working:
$ cd NightClub/
$ ./NightClub.pl
>> Listening on 127.0.0.1:3000
== Entering the development dance floor ...
That’s good, but here we want to brutalize a bit the application in order to trigger an exception, so let’s do some buggy code in the main route handler of NightClub.pm
package NightClub;
use Dancer;
use Template;
get '/' => sub {
Travolta->dance;
template 'index';
};
We call the Travolta module, but hey, it hasn’t been imported yet, Perl won’t like it.
After restarting the application, we can see that output. Dancer shows us the catched error message, the source code from where it was sent, the caller stack and dumps out all the settings and environment variables.
This is pretty handy in development phase, but we clearly don’t want to show that to the real world when our application will reach a production phase.
Not a problem at all, Dancer supports environements: it loads a YAML file for settings initialization at startup, that file depending on the chosen environement. If not specified, the environement is the “development” one, so environments/development.yml gets loaded.
In that file, the setting “show_errors” is set to true, hence the error output show above. But in the “production” configuration file, guess what, show_errors is set to false.
If we run the application in production environement…
$ ./NightClub.pl -e production
>> Listening on 127.0.0.1:3000
== Entering the production dance floor ...
… we get that output, which is clearly less chatty than the previous one.
Of course this is the default error page built on-the-fly by Dancer. If a file public/500.html exists, then Dancer will use it instead.
Now that errors are correctly handled, I will focus on the session/cookies support and then, will be close to release 1.0 \o/