| 1 | package Articulate::Error; | ||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Articulate::Error - represent an error or exception in processing a | ||||||||||
| 8 | request | ||||||||||
| 9 | |||||||||||
| 10 | =cut | ||||||||||
| 11 | |||||||||||
| 12 | 11 | ▶ | use Module::Load; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 11 | ▶ | use overload '""' => | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
| |||||||||||
| 14 | 0 | ▶ | sub { my $self = shift; $self->http_code() . ' ' . $self->simple_message }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.6
| |||||||||||
| 15 | |||||||||||
| 16 | 11 | ▶ | use Exporter::Declare; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 11 | default_exports qw(throw_error); | |||||||||
| 18 | |||||||||||
| 19 | =head1 FUNCTIONS | ||||||||||
| 20 | |||||||||||
| 21 | =head3 throw_error | ||||||||||
| 22 | |||||||||||
| 23 | throw_error 'Forbidden'; | ||||||||||
| 24 | throw_error NotFound => "I don't want to alarm you, but it seems to be missiong"; | ||||||||||
| 25 | |||||||||||
| 26 | This creates an error of the type provided and throws it immediately. | ||||||||||
| 27 | These are things like C<Articulate::Error::Forbidden>. | ||||||||||
| 28 | |||||||||||
| 29 | =cut | ||||||||||
| 30 | |||||||||||
| 31 | 16 | ▶ | sub throw_error { | ||||||||
|
Subroutine
throw_error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 32 | 16 | new_error(@_)->throw; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 16 | ▶ | sub new_error { | ||||||||
|
Subroutine
new_error: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||
| 36 | 16 | my ( $type, $message ) = @_; | |||||||||
| 37 | 16 | ▶ | my $class = __PACKAGE__ . ( $type ? '::' . $type : '' ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 16 | ▶ | $class->new( { ( $message ? ( simple_message => $message ) : () ) } ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 11 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 11 | with 'Throwable'; | |||||||||
| 43 | 11 | with 'StackTrace::Auto'; | |||||||||
| 44 | |||||||||||
| 45 | =head1 METHODS | ||||||||||
| 46 | |||||||||||
| 47 | =head3 new | ||||||||||
| 48 | |||||||||||
| 49 | An ordinary Moo constructor. | ||||||||||
| 50 | |||||||||||
| 51 | =head3 throw | ||||||||||
| 52 | |||||||||||
| 53 | Implements the C<Throwable> role - basically C<< die | ||||||||||
| 54 | __PACKAGE__->new(@_) >>. | ||||||||||
| 55 | |||||||||||
| 56 | =head1 ATTRIBUTES | ||||||||||
| 57 | |||||||||||
| 58 | =head3 simple_message | ||||||||||
| 59 | |||||||||||
| 60 | Be kind and let the user know what happened, in summary. Default is 'An | ||||||||||
| 61 | unknown error has occurred'. | ||||||||||
| 62 | |||||||||||
| 63 | That said, do consider whether this is the right place to put | ||||||||||
| 64 | potentially sensitive diagnostic information. | ||||||||||
| 65 | |||||||||||
| 66 | =head3 http_code | ||||||||||
| 67 | |||||||||||
| 68 | The equivalent status code. | ||||||||||
| 69 | |||||||||||
| 70 | Defaults to 500, always an integer. | ||||||||||
| 71 | |||||||||||
| 72 | =head3 caller | ||||||||||
| 73 | |||||||||||
| 74 | Tries to take a sensible guess at where in your code this was actually | ||||||||||
| 75 | thrown from. This may vary, don't rely on it! | ||||||||||
| 76 | |||||||||||
| 77 | =cut | ||||||||||
| 78 | |||||||||||
| 79 | has simple_message => ( | ||||||||||
| 80 | is => 'rw', | ||||||||||
| 81 | 0 | ▶ | default => sub { 'An unknown error has occurred' }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 82 | 11 | ); | |||||||||
| 83 | |||||||||||
| 84 | has http_code => ( | ||||||||||
| 85 | is => 'rw', | ||||||||||
| 86 | 0 | ▶ | default => sub { 500 }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 87 | 16 | ▶ | coerce => sub { 0 + shift } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 88 | 11 | ); | |||||||||
| 89 | |||||||||||
| 90 | has caller => ( | ||||||||||
| 91 | is => 'rw', | ||||||||||
| 92 | 16 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||
| 93 | 16 | ▶ | ( [ caller(0) ]->[0] =~ m/Throwable/ ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | ? [ 'hmm', caller(2) ] | ||||||||||
| 95 | : [ caller(1) ]; | ||||||||||
| 96 | } | ||||||||||
| 97 | 11 | ); | |||||||||
| 98 | |||||||||||
| 99 | # This needs to go at the end, because of Class::XSAccessor stuff | ||||||||||
| 100 | |||||||||||
| 101 | 11 | Module::Load::load( __PACKAGE__ . '::' . $_ ) for qw( | |||||||||
| 102 | BadRequest | ||||||||||
| 103 | Forbidden | ||||||||||
| 104 | Internal | ||||||||||
| 105 | NotFound | ||||||||||
| 106 | Unauthorised | ||||||||||
| 107 | AlreadyExists | ||||||||||
| 108 | ); | ||||||||||
| 109 | |||||||||||
| 110 | 11 | 1; | |||||||||