So you've launched your brand new Catalyst
site in production but your test suite is not complete (or present) and
you expect a few glitches here and there. You want to be the first to
know when an error happens so checking the error_log file once in a
while is out of the question.
Here is what you can do about it :
Override the finalize_error method and make it email the nice Catalyst error page to you. I assume debugging is disabled on a production server so we have to temporarily enable it to fool Catalyst into generating the big nice debug page. This way you don't have to use your own email template and fill it with session,stash,flash and all kind of hashes. On the other hand you might want to change the short error message (non-debug one) to something more personalised, but that in a future post.
Also add the Email module if it's not on the used modules list already and configure the address to send the debug info to.
Later edit: this is now outdated and I'm sure it can be done in a more elegant way (e.g. using Catalyst::Action::RenderView::ErrorHandler) so I'm open to suggestions.
Here is what you can do about it :
Override the finalize_error method and make it email the nice Catalyst error page to you. I assume debugging is disabled on a production server so we have to temporarily enable it to fool Catalyst into generating the big nice debug page. This way you don't have to use your own email template and fill it with session,stash,flash and all kind of hashes. On the other hand you might want to change the short error message (non-debug one) to something more personalised, but that in a future post.
Also add the Email module if it's not on the used modules list already and configure the address to send the debug info to.
use Catalyst qw/...other_modules_here_and_then... Email/;
# Set here where the emails should go or add it in your
# appname.yml config file if you are using ConfigLoader
__PACKAGE__->config(
admin_email => 'your@email.com',
);
sub finalize_error {
my $c = shift;
my @args = @_;
# This behaviour might change !
my $class = ref $c;
my $error_output;
unless ( $c->debug ) {
no strict 'refs';
no warnings 'redefine';
*{ "$class\::debug"} = sub { 1 };
$c->NEXT::finalize_error($c, @args);
$error_output = $c->response->output;
*{ "$class\::debug"} = sub { 0 };
};
$c->NEXT::finalize_error($c, @args);
$error_output ||= $c->response->output;
$c->email(
header => [
From => $c->config->{name} . '@error.com',
To => $c->config->{admin_email},
Subject => $c->config->{name} . " error",
],
attributes => {
content_type => "text/html",
},
body => $error_output,
);
};
Later edit: this is now outdated and I'm sure it can be done in a more elegant way (e.g. using Catalyst::Action::RenderView::ErrorHandler) so I'm open to suggestions.
