| 1 | package CatalystX::Utils::HttpException; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | with 'CatalystX::Utils::DoesHttpException'; | ||
| 7 | |||
| 8 | sub import { | ||
| 9 | my $class = shift; | ||
| 10 | my @imports = @_; | ||
| 11 | my $target = caller; | ||
| 12 | |||
| 13 | foreach my $import_sub (@imports) { | ||
| 14 | if( ($import_sub eq 'throw_http') && (!$target->can('throw_http')) ) { | ||
| 15 | eval qq[ | ||
| 16 | package $target; | ||
| 17 | use Carp; | ||
| 18 | |||
| 19 | sub throw_http { | ||
| 20 | my (\$status, \%args) = \@_; | ||
| 21 | croak \$class->new(\%args, status_code => \$status); | ||
| 22 | } | ||
| 23 | ]; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | __PACKAGE__->meta->make_immutable; | ||
| 29 | |||
| 30 | =head1 NAME | ||
| 31 | |||
| 32 | CatalystX::Utils::HttpException - A basic way to throw exceptions | ||
| 33 | |||
| 34 | =head1 SYNOPSIS | ||
| 35 | |||
| 36 | use CatalystX::Utils::HttpException 'throw_http'; | ||
| 37 | |||
| 38 | throw_http $code, %extra; | ||
| 39 | |||
| 40 | ## OR ## | ||
| 41 | |||
| 42 | CatalystX::Utils::HttpException->throw(500, %extra); | ||
| 43 | |||
| 44 | ## OR Subclass for your use case (although just consuming the role 'CatalystX::Utils::DoesHttpException' | ||
| 45 | ## is probably cleaner | ||
| 46 | |||
| 47 | package MyApp::Exception::Custom; | ||
| 48 | |||
| 49 | use Moose; | ||
| 50 | extends 'CatalystX::Utils::HttpException'; | ||
| 51 | |||
| 52 | sub status_code { 418 } | ||
| 53 | sub error { 'Coffee not allowed' } | ||
| 54 | |||
| 55 | |||
| 56 | =head1 DESCRIPTION | ||
| 57 | |||
| 58 | If you need to throw an exception from code called by L<Catalyst>, such as code deep | ||
| 59 | inside your L<DBIx::Class> classes and you want to signal how to handle the issue | ||
| 60 | you an use this. You can also use this to subclass your own custom messages that will | ||
| 61 | get properly handled in a web context. | ||
| 62 | |||
| 63 | This class is semi deprecated (not recommended anymore for creating custom exception classes | ||
| 64 | and you should use the role L<CatalystX::Utils::DoesHttpException> which this consumes | ||
| 65 | directly. | ||
| 66 | |||
| 67 | =head1 SEE ALSO | ||
| 68 | |||
| 69 | L<CatalystX::Errors>. | ||
| 70 | |||
| 71 | =head1 AUTHOR | ||
| 72 | |||
| 73 | L<CatalystX::Errors>. | ||
| 74 | |||
| 75 | =head1 COPYRIGHT & LICENSE | ||
| 76 | |||
| 77 | L<CatalystX::Errors>. | ||
| 78 | |||
| 79 | =cut |