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.3520';
5 205     236   67991 use strict;
  205         496  
  205         5814  
6 205     205   1148 use warnings;
  205         413  
  205         4772  
7 205     205   1038 use Carp;
  205         451  
  205         11471  
8 205     205   1433 use Scalar::Util qw(blessed);
  205         483  
  205         16046  
9              
10             our $Verbose = 0;
11              
12 205     205   94095 use Dancer::Exception::Base;
  205         615  
  205         10382  
13              
14 205     205   1470 use base qw(Exporter);
  205         602  
  205         24404  
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   106551 use Try::Tiny ();
  205         442842  
  205         163425  
20              
21             sub try (&;@) {
22 1699     1699 1 7174 goto &Try::Tiny::try;
23             }
24              
25             sub catch (&;@) {
26 1086     1086 1 7997 my ( $block, @rest ) = @_;
27              
28 1086         1529 my $continuation_code;
29 1086 50       2005 my @new_rest = grep { ref ne 'Try::Tiny::Catch' or $continuation_code = $$_, 0 } @rest;
  1         10  
30             $continuation_code
31             and return ( bless( \ sub {
32 1 50 33 1   66 ref && blessed($_) && $_->isa('Dancer::Continuation')
33             ? $continuation_code->(@_) : $block->(@_);
34 1086 100       2552 }, 'Try::Tiny::Catch') , @new_rest);
35              
36             return ( bless ( \ sub {
37 67 100 100 67   558 ref && blessed($_) && $_->isa('Dancer::Continuation')
38             ? die($_) : $block->(@_) ;
39 1085         6478 }, 'Try::Tiny::Catch'), @new_rest );
40             }
41              
42             sub continuation (&;@) {
43 1693     1693 1 5014 my ( $block, @rest ) = @_;
44              
45 1693         2295 my $catch_code;
46 1693 50       2819 my @new_rest = grep { ref ne 'Try::Tiny::Catch' or $catch_code = $$_, 0 } @rest;
  1080         5816  
47             $catch_code
48             and return ( bless( \ sub {
49 197 100 100 197   5128 ref && blessed($_) && $_->isa('Dancer::Continuation')
50             ? $block->(@_) : $catch_code->(@_);
51 1693 100       7670 }, 'Try::Tiny::Catch') , @new_rest);
52              
53             return ( bless ( \ sub {
54 38 100 100 38   1263 ref && blessed($_) && $_->isa('Dancer::Continuation')
55             ? $block->(@_) : die($_);
56 613         3961 }, 'Try::Tiny::Catch'), @new_rest );
57             }
58              
59             sub raise ($;@) {
60 43     43 1 749 my $exception_name = shift;
61 43         89 my $exception;
62 43 50       189 if ($exception_name =~ s/^\+//) {
63 0         0 $exception = $exception_name->new(@_);
64             } else {
65 43         164 _camelize($exception_name);
66 43         749 $exception = "Dancer::Exception::$exception_name"->new(@_);
67             }
68 41         229 $exception->throw();
69             }
70              
71             sub _camelize {
72             # using aliasing for ease of use
73 43     43   321 $_[0] =~ s/^(.)/uc($1)/e;
  43         195  
74 43         189 $_[0] =~ s/_(.)/'::' . uc($1)/eg;
  28         142  
75             }
76              
77             sub register_exception {
78 3890     3890 1 14059 my ($exception_name, %params) = @_;
79 3890         7895 my $exception_class = 'Dancer::Exception::' . $exception_name;
80 3890         5852 my $path = $exception_class; $path =~ s|::|/|g; $path .= '.pm';
  3890         16546  
  3890         6899  
81              
82 3890 50       10751 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         6184 my $message_pattern = $params{message_pattern};
90 3890         5272 my $composed_from = $params{composed_from};
91 3890         7132 my @composition = map { 'Dancer::Exception::' . $_ } @$composed_from;
  3490         10301  
92              
93 3890         10698 $INC{$path} = __FILE__;
94 3890         247759 eval "\@${exception_class}::ISA=qw(Dancer::Exception::Base " . join (' ', @composition) . ');';
95              
96 3890 50       19099 if (defined $message_pattern) {
97 205     205   1807 no strict 'refs';
  205         516  
  205         70444  
98 3890     60   14245 *{"${exception_class}::_message_pattern"} = sub { $message_pattern };
  3890         32313  
  60         160  
99             }
100              
101             }
102              
103             sub registered_exceptions {
104 2     2 1 865 sort map { s|/|::|g; s/\.pm$//; $_ } grep { s|^Dancer/Exception/||; } keys %INC;
  43         83  
  43         100  
  43         99  
  255         422  
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__