File Coverage

blib/lib/XAO/Errors.pm
Criterion Covered Total %
statement 94 103 91.2
branch 13 24 54.1
condition 1 3 33.3
subroutine 26 28 92.8
pod 0 2 0.0
total 134 160 83.7


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             XAO::Errors - throwable errors namespace support
4              
5             =head1 SYNOPSIS
6              
7             package XAO::Fubar;
8             use XAO::Errors qw(XAO::Fubar);
9              
10             sub foo {
11             ...
12             throw XAO::E::Fubar "foo - wrong arguments";
13             }
14              
15             =head1 DESCRIPTION
16              
17             Magic module that creates error namespaces for caller's. Should be
18             used in situations like that. Say you create a XAO module called
19             XAO::DO::Data::Product and want to throw errors from it. In order for
20             these errors to be distinguishable you need separate namespace for
21             them -- that's where XAO::Errors comes to rescue.
22              
23             In the bizarre case when you want more then one namespace for
24             errors - you can pass these namespaces into XAO::Errors and it will
25             make them throwable. It does not matter what to pass to XAO::Errors -
26             the namespace of an error or the namespace of the package, the result
27             would always go into XAO::E namespace.
28              
29             =cut
30              
31             ###############################################################################
32             package XAO::Errors;
33 11     11   74 use strict;
  11         15  
  11         301  
34 11     11   3895 use Error;
  11         26284  
  11         50  
35              
36 11     11   668 use vars qw($VERSION);
  11         21  
  11         1138  
37             $VERSION=(0+sprintf('%u.%03u',(q$Id: Errors.pm,v 2.1 2005/01/13 22:34:34 am Exp $ =~ /\s(\d+)\.(\d+)\s/))) || die "Bad VERSION";
38              
39 9     9   37 use vars qw(%errors_cache);
  9         12  
  9         3342  
40              
41             sub load_e_class ($) {
42 45     45 0 63 my $module=shift;
43 45         66 my $em;
44 45 100       300 if($module=~/^XAO::E((::\w+)+)$/) {
    50          
45 4         20 $em=$module;
46 4         21 $module='XAO' . $1;
47             }
48             elsif($module=~/^XAO((::\w+)+)$/) {
49 41         136 $em='XAO::E' . $1;
50             }
51             else {
52 0         0 throw Error::Simple "Can't import error module for $module";
53             }
54              
55 45 100       442 return $em if $errors_cache{$em};
56              
57 9 50   9   45 eval <
  9 50   9   21  
  9 50   9   159  
  9 0   8   26  
  9 0   8   12  
  9     8   32  
  9     8   475  
  9     8   11  
  9     8   758  
  8     8   43  
  8     8   9  
  8     8   171  
  8     4   29  
  8     4   8  
  8     4   27  
  8     1   485  
  8     3   10  
  8     1   746  
  8     0   33  
  8     0   10  
  8         167  
  8         37  
  8         8  
  8         25  
  8         521  
  8         9  
  8         880  
  8         30  
  8         33  
  8         227  
  8         25  
  8         10  
  8         51  
  8         551  
  8         13  
  8         810  
  4         19  
  4         5  
  4         80  
  4         12  
  4         5  
  4         21  
  4         239  
  4         4  
  4         512  
  39         3579  
  1         188  
  1         9  
  1         6  
  1         12  
  3         15  
  3         9  
  3         22  
  3         39  
  1         2  
  1         3  
  1         8  
  1         10  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
58              
59             package $em;
60             use strict;
61             use Error;
62             use vars qw(\@ISA);
63             \@ISA=qw(Error::Simple);
64              
65             sub throw {
66             my \$self=shift;
67             my \$text=join('',map { defined(\$_) ? \$_ : '' } \@_);
68             \$self->SUPER::throw('${module}::' . \$text);
69             }
70              
71             1;
72             END
73 39 50       106 throw Error::Simple $@ if $@;
74 39         91 $errors_cache{$em}=1;
75              
76 39         2717 return $em;
77             }
78              
79             sub import {
80 47     47   688 my $class=shift;
81 47         111 my @list=@_;
82              
83 47         2045 foreach my $module (@list) {
84 44         103 load_e_class($module);
85             }
86             }
87              
88             sub throw_by_class ($$$) {
89              
90 4 100 33 4 0 19 @_==2 || @_==3 ||
91             throw Error::Simple "throw_by_class - number of arguments is not 2 or 3";
92              
93 4 50       34 my $self=(@_==3) ? shift : 'XAO::Errors';
94 4         7 my $class=shift;
95 4 50       11 $class=ref($class) if ref($class);
96              
97 4         4 my $text=shift;
98              
99 4         14 my $em=load_e_class($class);
100              
101             ##
102             # Most probably will screw up stack trace, need to check and fix!
103             #
104 9     9   49 no strict 'refs';
  9         11  
  9         534  
105 4         122 $em->throw($text);
106             }
107              
108             ###############################################################################
109             1;
110             __END__