File Coverage

blib/lib/Articulate/Error.pm
Criterion Covered Total %
statement 22 24 91.6
branch 2 4 50.0
condition n/a
subroutine 8 9 88.8
pod 1 2 50.0
total 33 39 84.6


line stmt bran cond sub pod time code
1             package Articulate::Error;
2 11     11   49 use strict;
  11         16  
  11         354  
3 11     11   45 use warnings;
  11         21  
  11         272  
4              
5             =head1 NAME
6              
7             Articulate::Error - represent an error or exception in processing a
8             request
9              
10             =cut
11              
12 11     11   41 use Module::Load;
  11         13  
  11         71  
13             use overload '""' =>
14 11     11   4037 sub { my $self = shift; $self->http_code() . ' ' . $self->simple_message };
  11     0   3122  
  11         150  
  0         0  
  0         0  
15              
16 11     11   647 use Exporter::Declare;
  11         21  
  11         52  
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 8     8 1 25 new_error(@_)->throw;
33             }
34              
35             sub new_error {
36 8     8 0 20 my ( $type, $message ) = @_;
37 8 50       30 my $class = __PACKAGE__ . ( $type ? '::' . $type : '' );
38 8 50       185 $class->new( { ( $message ? ( simple_message => $message ) : () ) } );
39             }
40              
41 11     11   12932 use Moo;
  11         21125  
  11         78  
42             with 'Throwable';
43             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 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             default => sub { 'An unknown error has occurred' },
82             );
83              
84             has http_code => (
85             is => 'rw',
86             default => sub { 500 },
87             coerce => sub { 0 + shift }
88             );
89              
90             has caller => (
91             is => 'rw',
92             default => sub {
93             ( [ caller(0) ]->[0] =~ m/Throwable/ )
94             ? [ 'hmm', caller(2) ]
95             : [ caller(1) ];
96             }
97             );
98              
99             # This needs to go at the end, because of Class::XSAccessor stuff
100              
101             Module::Load::load( __PACKAGE__ . '::' . $_ ) for qw(
102             BadRequest
103             Forbidden
104             Internal
105             NotFound
106             Unauthorised
107             AlreadyExists
108             );
109              
110             1;