File Coverage

blib/lib/Catalyst/Exception/Basic.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 3 3 100.0
total 26 26 100.0


line stmt bran cond sub pod time code
1             package Catalyst::Exception::Basic;
2              
3 181     181   179217 use Moose::Role;
  181         689416  
  181         1070  
4 181     181   999236 use Carp;
  181         571  
  181         14774  
5 181     181   1654 use namespace::clean -except => 'meta';
  181         575  
  181         1566  
6              
7             with 'Catalyst::Exception::Interface';
8              
9             has message => (
10             is => 'ro',
11             isa => 'Str',
12             default => sub { $! || '' },
13             );
14              
15             sub as_string {
16 236     236 1 471 my ($self) = @_;
17 236         6881 return $self->message;
18             }
19              
20             around BUILDARGS => sub {
21             my ($next, $class, @args) = @_;
22             if (@args == 1 && !ref $args[0]) {
23             @args = (message => $args[0]);
24             }
25              
26             my $args = $class->$next(@args);
27             $args->{message} ||= $args->{error}
28             if exists $args->{error};
29              
30             return $args;
31             };
32              
33             sub throw {
34 38     38 1 159 my $class = shift;
35 38         1266 my $error = $class->new(@_);
36 38         157 local $Carp::CarpLevel = 1;
37 38         1477 croak $error;
38             }
39              
40             sub rethrow {
41 68     68 1 167 my ($self) = @_;
42 68         1340 croak $self;
43             }
44              
45             1;
46              
47             =head1 NAME
48              
49             Catalyst::Exception::Basic - Basic Catalyst Exception Role
50              
51             =head1 SYNOPSIS
52              
53             package My::Exception;
54             use Moose;
55             use namespace::clean -except => 'meta';
56              
57             with 'Catalyst::Exception::Basic';
58              
59             # Elsewhere..
60             My::Exception->throw( qq/Fatal exception/ );
61              
62             See also L<Catalyst> and L<Catalyst::Exception>.
63              
64             =head1 DESCRIPTION
65              
66             This is the basic Catalyst Exception role which implements all of
67             L<Catalyst::Exception::Interface>.
68              
69             =head1 ATTRIBUTES
70              
71             =head2 message
72              
73             Holds the exception message.
74              
75             =head1 METHODS
76              
77             =head2 as_string
78              
79             Stringifies the exception's message attribute.
80             Called when the object is stringified by overloading.
81              
82             =head2 throw( $message )
83              
84             =head2 throw( message => $message )
85              
86             =head2 throw( error => $error )
87              
88             Throws a fatal exception.
89              
90             =head2 rethrow( $exception )
91              
92             Rethrows a caught exception.
93              
94             =head2 meta
95              
96             Provided by Moose
97              
98             =head1 AUTHORS
99              
100             Catalyst Contributors, see Catalyst.pm
101              
102             =head1 COPYRIGHT
103              
104             This library is free software. You can redistribute it and/or modify
105             it under the same terms as Perl itself.
106              
107             =cut