| 1 | package Apache2::Controller::X; | ||
| 2 | 3 | ▶ | use warnings FATAL => 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Apache2::Controller::X - Exception::Class hierarchy for Apache2::Controller | ||
| 8 | |||
| 9 | =head1 VERSION | ||
| 10 | |||
| 11 | Version 1.001.001 | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | 3 | ▶ | use version; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 3 | our $VERSION = version->new('1.001.001'); | |
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | package MyApp::C::Foo; | ||
| 21 | use base qw( Apache2::Controller MyApp::Blabber ); | ||
| 22 | use Apache2::Controller::X; | ||
| 23 | # ... | ||
| 24 | sub some_page_controller_method { | ||
| 25 | my ($self, @path_args) = @_; | ||
| 26 | $self->print( $self->blabber() || a2cx "Cannot blabber: $OS_ERROR" ); | ||
| 27 | } | ||
| 28 | |||
| 29 | # or subclass and extend the errors... | ||
| 30 | |||
| 31 | package MyApp::X; | ||
| 32 | use base qw( Apache2::Controller::X ); | ||
| 33 | use Exception::Class ( | ||
| 34 | 'MyApp::X' => { | ||
| 35 | isa => 'Apache2::Controller::X', | ||
| 36 | fields => [qw( message status dump action )], | ||
| 37 | }, | ||
| 38 | alias => 'myx', | ||
| 39 | ); | ||
| 40 | |||
| 41 | package MyApp::C::Bar; | ||
| 42 | use base qw( Apache2::Controller ); | ||
| 43 | use Apache2::Const -compile => qw( :http ); | ||
| 44 | use MyApp::X; | ||
| 45 | # ... | ||
| 46 | sub page_controller_method { | ||
| 47 | myx message => q{ You're not supposed to be here. }, | ||
| 48 | status => Apache2::Const::FORBIDDEN, | ||
| 49 | action => sub {"not sure how you'd implement this actually"}, | ||
| 50 | dump => { | ||
| 51 | this => q{structure will get YAML::Syck::Dump'd}, | ||
| 52 | that => [qw( to the error log )], | ||
| 53 | }; | ||
| 54 | } | ||
| 55 | |||
| 56 | TODO: when $X is intercepted by handler() in each of the controller layers, | ||
| 57 | attach it to pnotes. | ||
| 58 | |||
| 59 | =head1 DESCRIPTION | ||
| 60 | |||
| 61 | Hierarchy of L<Exception::Class> objects for L<Apache2::Controller>. | ||
| 62 | All are subclasses of Apache2::Controller::X. | ||
| 63 | |||
| 64 | =head1 FIELDS | ||
| 65 | |||
| 66 | All Apache2::Controller::X exceptions implement three fields: | ||
| 67 | |||
| 68 | =head2 message | ||
| 69 | |||
| 70 | Required. | ||
| 71 | The standard L<Exception::Class> message field. If you call C<throw()> | ||
| 72 | or the alias C<a2cx()> | ||
| 73 | with only one argument, a string, then this gets set as the message | ||
| 74 | field, which is displayed when the object is referred to in string context. | ||
| 75 | |||
| 76 | eval { a2cx "booyeah" }; | ||
| 77 | if (my $X = Exception::Class->caught('Apache2::Controller::X')) { | ||
| 78 | warn "my exception 'message' was '$X'\n"; | ||
| 79 | warn $X->trace; | ||
| 80 | } | ||
| 81 | |||
| 82 | =head2 status | ||
| 83 | |||
| 84 | This can be set to an L<Apache2::Const/:http> constant, which | ||
| 85 | will then be set as the status for the request. | ||
| 86 | |||
| 87 | a2cx message => "oh no!", | ||
| 88 | status => Apache2::Const::HTTP_INTERNAL_SERVER_ERROR; | ||
| 89 | |||
| 90 | =head2 status_line | ||
| 91 | |||
| 92 | Combined with status, when intercepted by L<Apache2::Controller/handler> | ||
| 93 | this sets a custom message with L<Apache2::RequestRec/status_line>. | ||
| 94 | |||
| 95 | a2cx message => "Warp injection coil failure in unit 3-A-73", | ||
| 96 | status => Apache2::Const::HTTP_INTERNAL_SERVER_ERROR, | ||
| 97 | status_line => "Turbulence ahead. Please buckle your safety belts."; | ||
| 98 | |||
| 99 | This differentiation can be used to display technical information | ||
| 100 | in the log while giving a nice message to the user. | ||
| 101 | |||
| 102 | If L<Apache2::Controller::Render::Template/error> is used, | ||
| 103 | status_line is preferentially used to translate the error code, | ||
| 104 | otherwise it uses L<HTTP::Status/status_message>. | ||
| 105 | |||
| 106 | =head2 dump | ||
| 107 | |||
| 108 | An arbitrary data structure which Apache2::Controller will send | ||
| 109 | through L<YAML::Syck> Dump() when printing to the error log. | ||
| 110 | |||
| 111 | =head1 SUBCLASSES | ||
| 112 | |||
| 113 | =head2 Apache2::Controller::X | ||
| 114 | |||
| 115 | The basic exception object that implements the three basic fields. | ||
| 116 | |||
| 117 | After abandoning redirects, I have no use for any subclasses. | ||
| 118 | Should not re-invent the wheel, after all. Just set the | ||
| 119 | outgoing location header and return REDIRECT from a controller | ||
| 120 | if you want to do that. Or other actions should be done. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | 3 | ▶ | use base qw( Exporter ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 125 | 3 | our @EXPORT = qw( a2cx ); | |
| 126 | |||
| 127 | 3 | ▶ | use Exception::Class ( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 128 | 3 | 'Apache2::Controller::X' => { | |
| 129 | alias => 'a2cx', | ||
| 130 | fields => [qw( message dump status status_line )], | ||
| 131 | }, | ||
| 132 | 3 | ); # whoopdeedoo | |
| 133 | |||
| 134 | =head1 METHODS | ||
| 135 | |||
| 136 | =head2 Fields | ||
| 137 | |||
| 138 | This is the Fields() method provided by L<Exception::Class>. | ||
| 139 | For some reason the pod test wants this method enumerated. | ||
| 140 | |||
| 141 | =head1 SEE ALSO | ||
| 142 | |||
| 143 | L<Exception::Class> | ||
| 144 | |||
| 145 | L<Apache2::Controller> | ||
| 146 | |||
| 147 | L<Apache2::Controller::NonResponseBase> | ||
| 148 | |||
| 149 | L<Apache2::Controller::NonResponseRequest> | ||
| 150 | |||
| 151 | =head1 AUTHOR | ||
| 152 | |||
| 153 | Mark Hedges, C<< <hedges ||at formdata.biz> >> | ||
| 154 | |||
| 155 | =head1 COPYRIGHT & LICENSE | ||
| 156 | |||
| 157 | Copyright 2008-2010 Mark Hedges, all rights reserved. | ||
| 158 | |||
| 159 | This program is free software; you can redistribute it and/or modify it | ||
| 160 | under the same terms as Perl itself. | ||
| 161 | |||
| 162 | This software is provided as-is, with no warranty | ||
| 163 | and no guarantee of fitness | ||
| 164 | for any particular purpose. | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | |||
| 169 | 3 | 1; | |