Articulate/Error.pm

filter: Statementstmt 88.5% 31 / 35 Branchbran 50.0% 3 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 9 / 12 Podpod 100% 1 / 1 totaltotal 81.4% 44 / 54 CC 4 SCAR 5.5
CC
4
Cov
83%
throw_error
3.2
__ANON__
2.1
__ANON__
2.0
CRAP 1.7
« Articulate/Validation.pm ↑ Summary Articulate/Construction.pm »
1 package Articulate::Error;
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶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 10 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 use overload '""' =>
14 10 ▶ 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
15  
16 10 ▶use Exporter::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 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 sub throw_error {
32 16 ▶ my ( $type, $message ) = @_;
Subroutine throw_error: called CC=3 SCAR=11.7
Pod documented
33 16 ▶ my $class = __PACKAGE__ . ( $type ? '::' . $type : '' );
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 16 0 16
34 16 ▶ $class->throw( { ( $message ? ( simple_message => $message ) : () ) } );
Branch: 1 branch
BranchTrueFalseTotal
$message ? : 16 0 16
35 }
36  
37 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 10 with 'Throwable';
39 10 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<Throwable> 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 0 ▶ default => sub { 'An unknown error has occurred' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 10 );
79  
80 has http_code => (
81 is => 'rw',
82 0 ▶ default => sub { 500 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 16 ▶ coerce => sub { 0 + shift }
Subroutine __ANON__: called CC=1 SCAR=0.0
84 10 );
85  
86 has caller => (
87 is => 'rw',
88 default => sub {
89 16 ▶ ( [ caller(0) ]->[0] =~ m/Throwable/ )
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
[CORE::caller 0]->[0] =~ /Throwable/ ? : 0 16 16
90 ? [ 'hmm', caller(2) ]
91 : [ caller(1) ];
92 }
93 10 );
94  
95 # This needs to go at the end, because of Class::XSAccessor stuff
96  
97 10 Module::Load::load( __PACKAGE__ . '::' . $_ ) for qw(
98 BadRequest
99 Forbidden
100 Internal
101 NotFound
102 Unauthorised
103 AlreadyExists
104 );
105  
106 10 1;
« Articulate/Validation.pm ↑ Summary Articulate/Construction.pm »