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.3514_04'; # TRIAL
5             $Dancer::Exception::VERSION = '1.351404';
6 206     232   57189 use strict;
  206         357  
  206         4835  
7 206     206   894 use warnings;
  206         323  
  206         3966  
8 206     206   927 use Carp;
  206         352  
  206         10089  
9 206     206   1165 use Scalar::Util qw(blessed);
  206         391  
  206         12489  
10              
11             our $Verbose = 0;
12              
13 206     206   76196 use Dancer::Exception::Base;
  206         449  
  206         8956  
14              
15 206     206   1333 use base qw(Exporter);
  206         328  
  206         21566  
16              
17             our @EXPORT_OK = (qw(try catch continuation register_exception registered_exceptions raise));
18             our %EXPORT_TAGS = ( all => \@EXPORT_OK );
19              
20 206     206   89667 use Try::Tiny ();
  206         356615  
  206         131569  
21              
22             sub try (&;@) {
23 1708     1708 1 6679 goto &Try::Tiny::try;
24             }
25              
26             sub catch (&;@) {
27 1092     1092 1 7989 my ( $block, @rest ) = @_;
28              
29 1092         1312 my $continuation_code;
30 1092 50       1668 my @new_rest = grep { ref ne 'Try::Tiny::Catch' or $continuation_code = $$_, 0 } @rest;
  1         7  
31             $continuation_code
32             and return ( bless( \ sub {
33 1 50 33 1   47 ref && blessed($_) && $_->isa('Dancer::Continuation')
34             ? $continuation_code->(@_) : $block->(@_);
35 1092 100       2032 }, 'Try::Tiny::Catch') , @new_rest);
36              
37             return ( bless ( \ sub {
38 67 100 100 67   496 ref && blessed($_) && $_->isa('Dancer::Continuation')
39             ? die($_) : $block->(@_) ;
40 1091         5489 }, 'Try::Tiny::Catch'), @new_rest );
41             }
42              
43             sub continuation (&;@) {
44 1702     1702 1 4222 my ( $block, @rest ) = @_;
45              
46 1702         1902 my $catch_code;
47 1702 50       2356 my @new_rest = grep { ref ne 'Try::Tiny::Catch' or $catch_code = $$_, 0 } @rest;
  1086         5002  
48             $catch_code
49             and return ( bless( \ sub {
50 197 100 100 197   4612 ref && blessed($_) && $_->isa('Dancer::Continuation')
51             ? $block->(@_) : $catch_code->(@_);
52 1702 100       6834 }, 'Try::Tiny::Catch') , @new_rest);
53              
54             return ( bless ( \ sub {
55 38 100 100 38   1122 ref && blessed($_) && $_->isa('Dancer::Continuation')
56             ? $block->(@_) : die($_);
57 616         3479 }, 'Try::Tiny::Catch'), @new_rest );
58             }
59              
60             sub raise ($;@) {
61 43     43 1 702 my $exception_name = shift;
62 43         83 my $exception;
63 43 50       155 if ($exception_name =~ s/^\+//) {
64 0         0 $exception = $exception_name->new(@_);
65             } else {
66 43         148 _camelize($exception_name);
67 43         488 $exception = "Dancer::Exception::$exception_name"->new(@_);
68             }
69 41         181 $exception->throw();
70             }
71              
72             sub _camelize {
73             # using aliasing for ease of use
74 43     43   234 $_[0] =~ s/^(.)/uc($1)/e;
  43         171  
75 43         164 $_[0] =~ s/_(.)/'::' . uc($1)/eg;
  28         94  
76             }
77              
78             sub register_exception {
79 3909     3909 1 12594 my ($exception_name, %params) = @_;
80 3909         6471 my $exception_class = 'Dancer::Exception::' . $exception_name;
81 3909         4809 my $path = $exception_class; $path =~ s|::|/|g; $path .= '.pm';
  3909         13762  
  3909         5638  
82              
83 3909 50       8666 if (exists $INC{$path}) {
84 0         0 local $Carp::CarpLevel = $Carp::CarpLevel++;
85 0         0 'Dancer::Exception::Base::Internal'
86             ->new("register_exception failed: $exception_name is already defined")
87             ->throw;
88             }
89              
90 3909         5253 my $message_pattern = $params{message_pattern};
91 3909         4399 my $composed_from = $params{composed_from};
92 3909         6010 my @composition = map { 'Dancer::Exception::' . $_ } @$composed_from;
  3507         8120  
93              
94 3909         8690 $INC{$path} = __FILE__;
95 3909         201757 eval "\@${exception_class}::ISA=qw(Dancer::Exception::Base " . join (' ', @composition) . ');';
96              
97 3909 50       15495 if (defined $message_pattern) {
98 206     206   1656 no strict 'refs';
  206         401  
  206         57515  
99 3909     60   11749 *{"${exception_class}::_message_pattern"} = sub { $message_pattern };
  3909         26825  
  60         135  
100             }
101              
102             }
103              
104             sub registered_exceptions {
105 2     2 1 871 sort map { s|/|::|g; s/\.pm$//; $_ } grep { s|^Dancer/Exception/||; } keys %INC;
  43         65  
  43         79  
  43         88  
  255         346  
106             }
107              
108             register_exception(@$_) foreach (
109             [ 'Core', message_pattern => 'core - %s' ],
110             [ 'Core::App', message_pattern => 'core - app - %s', composed_from => [ qw(Core) ] ],
111             [ 'Core::Config', message_pattern => 'core - config - %s', composed_from => [ qw(Core) ] ],
112             [ 'Core::Deprecation', message_pattern => 'core - deprecation - %s', composed_from => [ qw(Core) ] ],
113             [ 'Core::Engine', message_pattern => 'core - engine - %s', composed_from => [ qw(Core) ] ],
114             [ 'Core::Factory', message_pattern => 'core - factory - %s', composed_from => [ qw(Core) ] ],
115             [ 'Core::Factory::Hook', message_pattern => 'core - hook - %s', composed_from => [ qw(Core::Factory) ] ],
116             [ 'Core::Hook', message_pattern => 'core - hook - %s', composed_from => [ qw(Core) ] ],
117             [ 'Core::Fileutils', message_pattern => 'core - file utils - %s', composed_from => [ qw(Core) ] ],
118             [ 'Core::Handler', message_pattern => 'core - handler - %s', composed_from => [ qw(Core) ] ],
119             [ 'Core::Handler::PSGI', message_pattern => 'core - handler - %s', composed_from => [ qw(Core::Handler) ] ],
120             [ 'Core::Plugin', message_pattern => 'core - plugin - %s', composed_from => [ qw(Core) ] ],
121             [ 'Core::Renderer', message_pattern => 'core - renderer - %s', composed_from => [ qw(Core) ] ],
122             [ 'Core::Request', message_pattern => 'core - request - %s', composed_from => [ qw(Core) ] ],
123             [ 'Core::Route', message_pattern => 'core - route - %s', composed_from => [ qw(Core) ] ],
124             [ 'Core::Serializer', message_pattern => 'core - serializer - %s', composed_from => [ qw(Core) ] ],
125             [ 'Core::Template', message_pattern => 'core - template - %s', composed_from => [ qw(Core) ] ],
126             [ 'Core::Session', message_pattern => 'core - session - %s', composed_from => [ qw(Core) ] ],
127             );
128              
129             1;
130              
131             __END__