| 1 | package CatalystX::Utils::DoesHttpException; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | with 'Catalyst::Exception::Interface'; | ||
| 7 | |||
| 8 | has '_status_code' => (is=>'ro', init_arg=>'status_code', predicate=>'has_status_code'); | ||
| 9 | has '_error' => (is=>'ro', init_arg=>'error', predicate=>'has_error'); | ||
| 10 | has '_message' => (is=>'ro', init_arg=>'message', predicate=>'has_message'); | ||
| 11 | has '_additional_headers' => (is=>'ro', init_arg=>'additional_headers', predicate=>'has_additional_headers'); | ||
| 12 | |||
| 13 | sub status_code { | ||
| 14 | my $self = shift; | ||
| 15 | return $self->has_status_code ? $self->_status_code : 500; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub error { | ||
| 19 | my $self = shift; | ||
| 20 | return $self->has_error ? $self->_error : 'The system has generated unspecifed errors.'; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub message { | ||
| 24 | my $self = shift; | ||
| 25 | return $self->_message if $self->has_message; | ||
| 26 | return undef; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub additional_headers { | ||
| 30 | my $self = shift; | ||
| 31 | return $self->_additional_headers if $self->has_additional_headers; | ||
| 32 | return []; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub as_http_response { | ||
| 36 | my $self = shift; | ||
| 37 | my $message = $self->message; | ||
| 38 | my %template_args = (); | ||
| 39 | |||
| 40 | $template_args{message} = $message if $message; | ||
| 41 | $template_args{error} = $self->error; | ||
| 42 | |||
| 43 | return $self->status_code, $self->additional_headers, \%template_args; | ||
| 44 | } | ||
| 45 | |||
| 46 | # The following are required by 'Catalyst::Exception::Interface'. | ||
| 47 | |||
| 48 | sub as_string { | ||
| 49 | my ($self) = @_; | ||
| 50 | return $self->error; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub throw { | ||
| 54 | my $class = shift; | ||
| 55 | my (%args) = @_; | ||
| 56 | my $error = $class->new(%args); | ||
| 57 | local $Carp::CarpLevel = 1; | ||
| 58 | croak $error; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub rethrow { | ||
| 62 | my ($self) = @_; | ||
| 63 | croak $self; | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; | ||
| 67 | |||
| 68 | =head1 NAME | ||
| 69 | |||
| 70 | CatalystX::Utils::DoesHttpException - An exception role | ||
| 71 | |||
| 72 | =head1 SYNOPSIS | ||
| 73 | |||
| 74 | package MyApp::Exception::NoCoffee; | ||
| 75 | |||
| 76 | use Moose; | ||
| 77 | extends 'CatalystX::Utils::DoesHttpException'; | ||
| 78 | |||
| 79 | sub status_code { 418 } | ||
| 80 | sub error { 'Coffee not allowed' } | ||
| 81 | sub additional_headers { [ 'X-Error-Code' => '200101' ] } | ||
| 82 | |||
| 83 | =head1 DESCRIPTION | ||
| 84 | |||
| 85 | A L<Moose::Role> that does two things: 1) Create an exception object that L<Catalyst> | ||
| 86 | knows how to handle and 2) Tag that exception object with HTTP meta data so that L<CatalystX::Errors> | ||
| 87 | knows how to properly create a response to the client. | ||
| 88 | |||
| 89 | Sometimes you need to throw exceptions from classes that are external to L<Catalyst> (for | ||
| 90 | example L<DBIx::Class>). These are exceptions which are meant to be truely exceptional situations | ||
| 91 | for which no validation code or other business logic could catch (for example a sudden glitch in | ||
| 92 | the database) and for which we just need to display a meaningful error response to the user. In | ||
| 93 | the past most of these errors would fall into the generic '500 Server Error' bucket but this | ||
| 94 | approach lets you throw errors that are tagged with HTTP response information which allows you | ||
| 95 | to return a customized response that L<CatalystX::Errors> knows how to handle. | ||
| 96 | |||
| 97 | There are 4 fields related to the HTTP response and you can set each in one of two ways, either as | ||
| 98 | an init argument passed to ->new or as a custom subroutine in your consuming class. If you are | ||
| 99 | makeing custom expection objects it is likely you will be just overriding the methods but both | ||
| 100 | approaches are allowed: | ||
| 101 | |||
| 102 | =over | ||
| 103 | |||
| 104 | =item status_code | ||
| 105 | |||
| 106 | This is the HTTP status code. Defaults to 500 if not provided. Should be a 4xx or 5xx HTTP status | ||
| 107 | error code. | ||
| 108 | |||
| 109 | =item error | ||
| 110 | |||
| 111 | This is a string which is the text version of the error. This is not seen by the client but instead | ||
| 112 | goes to whatever you have handling the L<Catalyst> error log ($c->log->error($err)). Default is text | ||
| 113 | 'The system has generated unspecifed errors.'. | ||
| 114 | |||
| 115 | =item additional_headers | ||
| 116 | |||
| 117 | This is an arrayref of key / value pairs which are extra HTTP headers that wil be sent to the client. | ||
| 118 | Some HTTP error codes have required HTTP headers (like 401 Unauthorized should return a WWW-Authenticate | ||
| 119 | header that tells the client how to authenticate). Otherwise an optional field which just returns an | ||
| 120 | empty arrayref. | ||
| 121 | |||
| 122 | =item message | ||
| 123 | |||
| 124 | This is optional. This is the message that gets sent to the client as part of the response. If you | ||
| 125 | don't set this field then L<CatalystX::Errors> will use whatever the standard text of the error message | ||
| 126 | is for the C<status_code> you set via L<CatalystX::Utils::ErrorMessages>. You can override if you prefer | ||
| 127 | to control the error message yourself (or just write a custom view). | ||
| 128 | |||
| 129 | Remember if you choose to write a custom message you need to take care to properly scrub / HTML escape | ||
| 130 | any data that is coming from the client before using it in the response, else you are opening a HTML / | ||
| 131 | Javascript injection attack in your system. | ||
| 132 | |||
| 133 | =back | ||
| 134 | |||
| 135 | =head1 EXAMPLE | ||
| 136 | |||
| 137 | A more detailed example: | ||
| 138 | |||
| 139 | package Catalyst::ActionRole::Verbs::Utils::MethodNotAllowed; | ||
| 140 | |||
| 141 | use Moose; | ||
| 142 | use namespace::clean -except => 'meta'; | ||
| 143 | |||
| 144 | with 'CatalystX::Utils::DoesHttpException'; | ||
| 145 | |||
| 146 | has resource => (is=>'ro', required=>1); | ||
| 147 | has allowed_methods => (is=>'ro', isa=>'ArrayRef[Str]', required=>1); | ||
| 148 | has attempted_method => (is=>'ro', isa=>'Str', required=>1); | ||
| 149 | |||
| 150 | sub status { 405 } | ||
| 151 | sub error { "invalid method '@{[ $_[0]->attempted_method ]}' } | ||
| 152 | sub message { | ||
| 153 | "HTTP Method '@{[ $_[0]->attempted_method ]}' not permitted for resource '@{[ $_[0]->resource ]}'." . | ||
| 154 | "Can only be: @{[ join ', ', @{$_[0]->allowed_methods||[]} ]}"; | ||
| 155 | ); | ||
| 156 | |||
| 157 | __PACKAGE__->meta->make_immutable; | ||
| 158 | |||
| 159 | In this case the user would get a 405 error and the error will go to the error logs for tracking while the | ||
| 160 | more detailed 'message' would return to the user. Please note that you should scrub the values of 'allowed_methods' | ||
| 161 | since that's coming from the client and could be used as a type of HTML / Javascript injection attack. | ||
| 162 | |||
| 163 | =head1 SEE ALSO | ||
| 164 | |||
| 165 | L<CatalystX::Errors>. | ||
| 166 | |||
| 167 | =head1 AUTHOR | ||
| 168 | |||
| 169 | L<CatalystX::Errors>. | ||
| 170 | |||
| 171 | =head1 COPYRIGHT & LICENSE | ||
| 172 | |||
| 173 | L<CatalystX::Errors>. | ||
| 174 | |||
| 175 | =cut |