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