| 1 | package Catalyst::Plugin::CustomErrorMessage; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||
| 5 | Catalyst::Plugin::CustomErrorMessage - Catalyst plugin to have more "cute" error message. | ||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | use Catalyst qw( CustomErrorMessage ); | ||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | # optional (values in this example are the defaults) | ||||||||||||||||||||||||||||||||||
| 12 | __PACKAGE__->config->{'custom-error-message'}->{'uri-for-not-found'} = '/'; | ||||||||||||||||||||||||||||||||||
| 13 | __PACKAGE__->config->{'custom-error-message'}->{'error-template'} = 'error.tt2'; | ||||||||||||||||||||||||||||||||||
| 14 | __PACKAGE__->config->{'custom-error-message'}->{'content-type'} = 'text/html; charset=utf-8'; | ||||||||||||||||||||||||||||||||||
| 15 | __PACKAGE__->config->{'custom-error-message'}->{'view-name'} = 'TT'; | ||||||||||||||||||||||||||||||||||
| 16 | __PACKAGE__->config->{'custom-error-message'}->{'response-status'} = 500; | ||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | You can use this module if you want to get rid of: | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | (en) Please come back later | ||||||||||||||||||||||||||||||||||
| 23 | (fr) SVP veuillez revenir plus tard | ||||||||||||||||||||||||||||||||||
| 24 | (de) Bitte versuchen sie es spaeter nocheinmal | ||||||||||||||||||||||||||||||||||
| 25 | (at) Konnten's bitt'schoen spaeter nochmal reinschauen | ||||||||||||||||||||||||||||||||||
| 26 | (no) Vennligst prov igjen senere | ||||||||||||||||||||||||||||||||||
| 27 | (dk) Venligst prov igen senere | ||||||||||||||||||||||||||||||||||
| 28 | (pl) Prosze sprobowac pozniej | ||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | What it does is that it inherites finalize_error to $c object. | ||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | See finalize_error() function. | ||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | 2 | ▶ | use base qw{ Class::Data::Inheritable }; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | use HTML::Entities; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | use URI::Escape qw{ uri_escape_utf8 }; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | use MRO::Compat; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 43 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||
| 45 | 2 | our $VERSION = "0.06"; | |||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | =head2 finalize_error | ||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | In debug mode this function is skipped and user sees the original Catalyst error page in debug mode. | ||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | In "production" (non debug) mode it will return page with template set in | ||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | $c->config->{'custom-error-message'}->{'error-template'} | ||||||||||||||||||||||||||||||||||
| 57 | || | ||||||||||||||||||||||||||||||||||
| 58 | 'error.tt2' | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | $c->stash->{'finalize_error'} will be set to contain the error message. | ||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | For non existing resources (like misspelled url-s) if will do http redirect to | ||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | $c->uri_for( | ||||||||||||||||||||||||||||||||||
| 65 | $c->config->{'custom-error-message'}->{'uri-for-not-found'} | ||||||||||||||||||||||||||||||||||
| 66 | || | ||||||||||||||||||||||||||||||||||
| 67 | '/' | ||||||||||||||||||||||||||||||||||
| 68 | ) | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | $c->flash->{'finalize_error'} will be set to contain the error message. | ||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | To set different view name configure: | ||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | $c->config->{'custom-error-message'}->{'view-name'} = 'Mason'; | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | Content-type and response status can be configured via: | ||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | $c->config->{'custom-error-message'}->{'content-type'} = 'text/plain; charset=utf-8'; | ||||||||||||||||||||||||||||||||||
| 79 | $c->config->{'custom-error-message'}->{'response-status'} = 500; | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | 6 | ▶ | sub finalize_error { | ||||||||||||||||||||||||||||||||
|
Subroutine
finalize_error: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 84 | 6 | my $c = shift; | |||||||||||||||||||||||||||||||||
| 85 | 6 | ▶ | my $config = $c->config->{'custom-error-message'} || $c->config->{'custome-error-messsage'} || $c->config->{'custom-error-messsage'}; | ||||||||||||||||||||||||||||||||
|
Condition66%4/6
$c->config->{'custom-error-message'} || $c->config->{'custome-error-messsage'} || $c->config->{'custom-error-messsage'}
$c->config->{"custom-error-message"} or $c->config->{"custome-error-messsage"}
MC/DC33%1/3
$c->config->{'custom-error-message'} || $c->config->{'custome-error-messsage'} || $c->config->{'custom-error-messsage'}
$c->config->{"custom-error-message"}
$c->config->{"custome-error-messsage"}
$c->config->{'custom-error-messsage'}
Truth table50%2/4
A or B || C
A= $c->config->{"custom-error-message"}B= $c->config->{"custome-error-messsage"}C= $c->config->{'custom-error-messsage'}
| |||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | # in debug mode return the original "page" | ||||||||||||||||||||||||||||||||||
| 88 | 6 | ▶ | if ( $c->debug ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 89 | 1 | $c->maybe::next::method; | |||||||||||||||||||||||||||||||||
| 90 | 1 | return; | |||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | # create error string out of error array | ||||||||||||||||||||||||||||||||||
| 94 | 5 | my $error = join '<br/> ', map { encode_entities($_) } @{ $c->error }; | |||||||||||||||||||||||||||||||||
| 95 | 5 | ▶ | $error ||= 'No output'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$error ||= "No output"
MC/DC0%0/1
$error ||= "No output"
$error
Truth table50%1/2
A
A= $error
| |||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | # for wrong url that has no action associated do redirect | ||||||||||||||||||||||||||||||||||
| 98 | 5 | ▶ | if (not defined $c->action) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 99 | 2 | $c->flash->{'finalize_error'} = $error; | |||||||||||||||||||||||||||||||||
| 100 | 2 | $c->_save_flash(); # hack but must be called otherwise the flash data will be lost | |||||||||||||||||||||||||||||||||
| 101 | $c->response->redirect($c->uri_for( | ||||||||||||||||||||||||||||||||||
| 102 | 2 | ▶ | $config->{'uri-for-not-found'} | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'uri-for-not-found'} || '/'
MC/DC100%1/1
$config->{'uri-for-not-found'} || '/'
$config->{'uri-for-not-found'}
Truth table100%2/2
A
A= $config->{'uri-for-not-found'}
| |||||||||||||||||||||||||||||||||||
| 103 | || | ||||||||||||||||||||||||||||||||||
| 104 | '/' | ||||||||||||||||||||||||||||||||||
| 105 | )); | ||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||
| 107 | 2 | return; | |||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | # render the template | ||||||||||||||||||||||||||||||||||
| 111 | 3 | my $action_name = $c->action->reverse; | |||||||||||||||||||||||||||||||||
| 112 | 3 | $c->stash->{'finalize_error'} = $action_name.': '.$error; | |||||||||||||||||||||||||||||||||
| 113 | $c->response->content_type( | ||||||||||||||||||||||||||||||||||
| 114 | 3 | ▶ | $config->{'content-type'} | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'content-type'} || 'text/html; charset=utf-8'
MC/DC100%1/1
$config->{'content-type'} || 'text/html; charset=utf-8'
$config->{'content-type'}
Truth table100%2/2
A
A= $config->{'content-type'}
| |||||||||||||||||||||||||||||||||||
| 115 | || | ||||||||||||||||||||||||||||||||||
| 116 | 'text/html; charset=utf-8' | ||||||||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||||||||
| 118 | 3 | ▶ | my $view_name = $config->{'view-name'} || 'TT'; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'view-name'} || 'TT'
MC/DC100%1/1
$config->{'view-name'} || 'TT'
$config->{'view-name'}
Truth table100%2/2
A
A= $config->{'view-name'}
| |||||||||||||||||||||||||||||||||||
| 119 | 3 | eval { | |||||||||||||||||||||||||||||||||
| 120 | $c->response->body($c->view($view_name)->render($c, | ||||||||||||||||||||||||||||||||||
| 121 | 3 | ▶ | $config->{'error-template'} | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'error-template'} || 'error.tt2'
MC/DC100%1/1
$config->{'error-template'} || 'error.tt2'
$config->{'error-template'}
Truth table100%2/2
A
A= $config->{'error-template'}
| |||||||||||||||||||||||||||||||||||
| 122 | || | ||||||||||||||||||||||||||||||||||
| 123 | 'error.tt2' | ||||||||||||||||||||||||||||||||||
| 124 | )); | ||||||||||||||||||||||||||||||||||
| 125 | }; | ||||||||||||||||||||||||||||||||||
| 126 | 3 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 127 | 1 | $c->log->error($@); | |||||||||||||||||||||||||||||||||
| 128 | 1 | $c->maybe::next::method; | |||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||
| 131 | 3 | my $response_status = $config->{'response-status'}; | |||||||||||||||||||||||||||||||||
| 132 | 3 | ▶ | $response_status = 500 if not defined $response_status; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 133 | 3 | $c->response->status($response_status); | |||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | 2 | 1; | |||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||
| 140 | Jozef Kutej - E<lt>jkutej@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | Copyright (C) 2006 by Jozef Kutej | ||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||
| 147 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||||||||||||||||||||||||||||||||||
| 148 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||||||||||||||||