File Coverage

blib/lib/Articulate/Error.pm
Criterion Covered Total %
statement 21 23 91.3
branch 2 4 50.0
condition n/a
subroutine 7 8 87.5
pod 1 1 100.0
total 31 36 86.1


line stmt bran cond sub pod time code
1             package Articulate::Error;
2 10     10   48 use strict;
  10         14  
  10         338  
3 10     10   42 use warnings;
  10         26  
  10         255  
4              
5             =head1 NAME
6              
7             Articulate::Error - represent an error or exception in processing a
8             request
9              
10             =cut
11              
12 10     10   41 use Module::Load;
  10         15  
  10         67  
13             use overload '""' =>
14 10     10   5426 sub { my $self = shift; $self->http_code() . ' ' . $self->simple_message };
  10     0   3099  
  10         109  
  0         0  
  0         0  
15              
16 10     10   609 use Exporter::Declare;
  10         20  
  10         65  
17             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.
28              
29             =cut
30              
31             sub throw_error {
32 16     16 1 38 my ( $type, $message ) = @_;
33 16 50       104 my $class = __PACKAGE__ . ( $type ? '::' . $type : '' );
34 16 50       246 $class->throw( { ( $message ? ( simple_message => $message ) : () ) } );
35             }
36              
37 10     10   14090 use Moo;
  10         37781  
  10         66  
38             with 'Throwable';
39             with 'StackTrace::Auto';
40              
41             =head1 METHODS
42              
43             =head3 new
44              
45             An ordinary Moo constructor.
46              
47             =head3 throw
48              
49             Implements the C role - basically C<< die
50             __PACKAGE__->new(@_) >>.
51              
52             =head1 ATTRIBUTES
53              
54             =head3 simple_message
55              
56             Be kind and let the user know what happened, in summary. Default is 'An
57             unknown error has occurred'.
58              
59             That said, do consider whether this is the right place to put
60             potentially sensitive diagnostic information.
61              
62             =head3 http_code
63              
64             The equivalent status code.
65              
66             Defaults to 500, always an integer.
67              
68             =head3 caller
69              
70             Tries to take a sensible guess at where in your code this was actually
71             thrown from. This may vary, don't rely on it!
72              
73             =cut
74              
75             has simple_message => (
76             is => 'rw',
77             default => sub { 'An unknown error has occurred' },
78             );
79              
80             has http_code => (
81             is => 'rw',
82             default => sub { 500 },
83             coerce => sub { 0 + shift }
84             );
85              
86             has caller => (
87             is => 'rw',
88             default => sub {
89             ( [ caller(0) ]->[0] =~ m/Throwable/ )
90             ? [ 'hmm', caller(2) ]
91             : [ caller(1) ];
92             }
93             );
94              
95             # This needs to go at the end, because of Class::XSAccessor stuff
96              
97             Module::Load::load( __PACKAGE__ . '::' . $_ ) for qw(
98             BadRequest
99             Forbidden
100             Internal
101             NotFound
102             Unauthorised
103             AlreadyExists
104             );
105              
106             1;