File Coverage

blib/lib/Dancer/Exception.pm
Criterion Covered Total %
statement 72 75 96.0
branch 16 22 72.7
condition 10 12 83.3
subroutine 20 20 100.0
pod 6 6 100.0
total 124 135 91.8


line stmt bran cond sub pod time code
1             package Dancer::Exception;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: class for throwing and catching exceptions
4             $Dancer::Exception::VERSION = '1.3521';
5 205     234   68543 use strict;
  205         445  
  205         5674  
6 205     205   1119 use warnings;
  205         500  
  205         4727  
7 205     205   1075 use Carp;
  205         478  
  205         11248  
8 205     205   1428 use Scalar::Util qw(blessed);
  205         463  
  205         15878  
9              
10             our $Verbose = 0;
11              
12 205     205   92231 use Dancer::Exception::Base;
  205         677  
  205         10197  
13              
14 205     205   1394 use base qw(Exporter);
  205         636  
  205         23890  
15              
16             our @EXPORT_OK = (qw(try catch continuation register_exception registered_exceptions raise));
17             our %EXPORT_TAGS = ( all => \@EXPORT_OK );
18              
19 205     205   106483 use Try::Tiny ();
  205         433888  
  205         158037  
20              
21             sub try (&;@) {
22 1699     1699 1 7706 goto &Try::Tiny::try;
23             }
24              
25             sub catch (&;@) {
26 1086     1086 1 9762 my ( $block, @rest ) = @_;
27              
28 1086         1508 my $continuation_code;
29 1086 50       1904 my @new_rest = grep { ref ne 'Try::Tiny::Catch' or $continuation_code = $$_, 0 } @rest;
  1         9  
30             $continuation_code
31             and return ( bless( \ sub {
32 1 50 33 1   75 ref && blessed($_) && $_->isa('Dancer::Continuation')
33             ? $continuation_code->(@_) : $block->(@_);
34 1086 100       2492 }, 'Try::Tiny::Catch') , @new_rest);
35              
36             return ( bless ( \ sub {
37 67 100 100 67   566 ref && blessed($_) && $_->isa('Dancer::Continuation')
38             ? die($_) : $block->(@_) ;
39 1085         6297 }, 'Try::Tiny::Catch'), @new_rest );
40             }
41              
42             sub continuation (&;@) {
43 1693     1693 1 4992 my ( $block, @rest ) = @_;
44              
45 1693         2232 my $catch_code;
46 1693 50       2905 my @new_rest = grep { ref ne 'Try::Tiny::Catch' or $catch_code = $$_, 0 } @rest;
  1080         5760  
47             $catch_code
48             and return ( bless( \ sub {
49 197 100 100 197   5204 ref && blessed($_) && $_->isa('Dancer::Continuation')
50             ? $block->(@_) : $catch_code->(@_);
51 1693 100       7675 }, 'Try::Tiny::Catch') , @new_rest);
52              
53             return ( bless ( \ sub {
54 38 100 100 38   1275 ref && blessed($_) && $_->isa('Dancer::Continuation')
55             ? $block->(@_) : die($_);
56 613         3819 }, 'Try::Tiny::Catch'), @new_rest );
57             }
58              
59             sub raise ($;@) {
60 43     43 1 894 my $exception_name = shift;
61 43         86 my $exception;
62 43 50       182 if ($exception_name =~ s/^\+//) {
63 0         0 $exception = $exception_name->new(@_);
64             } else {
65 43         164 _camelize($exception_name);
66 43         626 $exception = "Dancer::Exception::$exception_name"->new(@_);
67             }
68 41         252 $exception->throw();
69             }
70              
71             sub _camelize {
72             # using aliasing for ease of use
73 43     43   284 $_[0] =~ s/^(.)/uc($1)/e;
  43         193  
74 43         191 $_[0] =~ s/_(.)/'::' . uc($1)/eg;
  28         141  
75             }
76              
77             sub register_exception {
78 3890     3890 1 14807 my ($exception_name, %params) = @_;
79 3890         7706 my $exception_class = 'Dancer::Exception::' . $exception_name;
80 3890         5800 my $path = $exception_class; $path =~ s|::|/|g; $path .= '.pm';
  3890         16912  
  3890         6794  
81              
82 3890 50       10731 if (exists $INC{$path}) {
83 0         0 local $Carp::CarpLevel = $Carp::CarpLevel++;
84 0         0 'Dancer::Exception::Base::Internal'
85             ->new("register_exception failed: $exception_name is already defined")
86             ->throw;
87             }
88              
89 3890         6182 my $message_pattern = $params{message_pattern};
90 3890         5068 my $composed_from = $params{composed_from};
91 3890         7124 my @composition = map { 'Dancer::Exception::' . $_ } @$composed_from;
  3490         9763  
92              
93 3890         10451 $INC{$path} = __FILE__;
94 3890         243349 eval "\@${exception_class}::ISA=qw(Dancer::Exception::Base " . join (' ', @composition) . ');';
95              
96 3890 50       19591 if (defined $message_pattern) {
97 205     205   1853 no strict 'refs';
  205         530  
  205         68880  
98 3890     60   14189 *{"${exception_class}::_message_pattern"} = sub { $message_pattern };
  3890         32011  
  60         170  
99             }
100              
101             }
102              
103             sub registered_exceptions {
104 2     2 1 1157 sort map { s|/|::|g; s/\.pm$//; $_ } grep { s|^Dancer/Exception/||; } keys %INC;
  43         78  
  43         99  
  43         100  
  255         421  
105             }
106              
107             register_exception(@$_) foreach (
108             [ 'Core', message_pattern => 'core - %s' ],
109             [ 'Core::App', message_pattern => 'core - app - %s', composed_from => [ qw(Core) ] ],
110             [ 'Core::Config', message_pattern => 'core - config - %s', composed_from => [ qw(Core) ] ],
111             [ 'Core::Deprecation', message_pattern => 'core - deprecation - %s', composed_from => [ qw(Core) ] ],
112             [ 'Core::Engine', message_pattern => 'core - engine - %s', composed_from => [ qw(Core) ] ],
113             [ 'Core::Factory', message_pattern => 'core - factory - %s', composed_from => [ qw(Core) ] ],
114             [ 'Core::Factory::Hook', message_pattern => 'core - hook - %s', composed_from => [ qw(Core::Factory) ] ],
115             [ 'Core::Hook', message_pattern => 'core - hook - %s', composed_from => [ qw(Core) ] ],
116             [ 'Core::Fileutils', message_pattern => 'core - file utils - %s', composed_from => [ qw(Core) ] ],
117             [ 'Core::Handler', message_pattern => 'core - handler - %s', composed_from => [ qw(Core) ] ],
118             [ 'Core::Handler::PSGI', message_pattern => 'core - handler - %s', composed_from => [ qw(Core::Handler) ] ],
119             [ 'Core::Plugin', message_pattern => 'core - plugin - %s', composed_from => [ qw(Core) ] ],
120             [ 'Core::Renderer', message_pattern => 'core - renderer - %s', composed_from => [ qw(Core) ] ],
121             [ 'Core::Request', message_pattern => 'core - request - %s', composed_from => [ qw(Core) ] ],
122             [ 'Core::Route', message_pattern => 'core - route - %s', composed_from => [ qw(Core) ] ],
123             [ 'Core::Serializer', message_pattern => 'core - serializer - %s', composed_from => [ qw(Core) ] ],
124             [ 'Core::Template', message_pattern => 'core - template - %s', composed_from => [ qw(Core) ] ],
125             [ 'Core::Session', message_pattern => 'core - session - %s', composed_from => [ qw(Core) ] ],
126             );
127              
128             1;
129              
130             __END__