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 9     9   43 use strict;
  9         12  
  9         322  
3 9     9   39 use warnings;
  9         20  
  9         243  
4              
5             =head1 NAME
6              
7             Articulate::Error - represent an error or exception in processing a request
8              
9             =cut
10              
11 9     9   37 use Module::Load;
  9         12  
  9         66  
12 9     9   4441 use overload '""' => sub { my $self = shift; $self->http_code() . ' ' . $self->simple_message };
  9     0   3472  
  9         95  
  0         0  
  0         0  
13              
14 9     9   568 use Exporter::Declare;
  9         11  
  9         52  
15             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.
25              
26             =cut
27              
28             sub throw_error {
29 16     16 1 32 my ( $type, $message ) = @_;
30 16 50       58 my $class = __PACKAGE__ . ( $type ? '::' . $type : '' );
31 16 50       189 $class->throw( { ( $message ? ( simple_message => $message) : () ) } );
32             };
33              
34 9     9   11567 use Moo;
  9         30517  
  9         73  
35             with 'Throwable';
36             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 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             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             coerce => sub { 0+shift };
76              
77             has caller =>
78             is => 'rw',
79             default => sub{ ( [caller(0)]->[0] =~ m/Throwable/) ? ['hmm',caller(2)] : [caller(1)] };
80              
81             # This needs to go at the end, because of Class::XSAccessor stuff
82              
83             Module::Load::load (__PACKAGE__.'::'.$_) for qw(
84             BadRequest
85             Forbidden
86             Internal
87             NotFound
88             Unauthorised
89             AlreadyExists
90             );
91              
92             1;