File Coverage

blib/lib/XAS/Lib/Mixins/Handlers.pm
Criterion Covered Total %
statement 3 51 5.8
branch 0 22 0.0
condition n/a
subroutine 1 5 20.0
pod 2 4 50.0
total 6 82 7.3


line stmt bran cond sub pod time code
1             package XAS::Lib::Mixins::Handlers;
2              
3             our $VERSION = '0.02';
4              
5             use XAS::Class
6 1         8 debug => 0,
7             version => $VERSION,
8             base => 'XAS::Base',
9             utils => ':validation compress',
10             mixins => 'exit_handler exception_handler error_handler parse_exception',
11 1     1   777 ;
  1         1  
12              
13             # ----------------------------------------------------------------------
14             # Public Methods
15             # ----------------------------------------------------------------------
16              
17             sub exception_handler {
18 0     0 0   my $self = shift;
19 0           my ($ex, $alias) = validate_params(\@_, [
20             1,
21             { optional => 1, default => undef },
22             ]);
23              
24 0           my $msg;
25 0           my ($errors, $rc) = $self->parse_exception($ex);
26              
27 0           $msg = $errors;
28 0 0         $msg = $self->message('exception', $alias, $errors) if (defined($alias));
29              
30 0           $self->log->error($msg);
31              
32 0 0         if ($self->env->alerts) {
33              
34 0           $self->alert->send($errors);
35              
36             }
37              
38             }
39              
40             sub exit_handler {
41 0     0 1   my $self = shift;
42 0           my ($ex, $alias) = validate_params(\@_, [
43             1,
44             { optional => 1, default => undef },
45             ]);
46              
47 0           my $msg;
48 0           my ($errors, $rc) = $self->parse_exception($ex);
49              
50 0           $msg = $errors;
51 0 0         $msg = $self->message('exception', $alias, $errors) if (defined($alias));
52              
53 0           $self->log->error($msg);
54              
55 0 0         if ($self->env->alerts) {
56              
57 0           $self->alert->send($errors);
58              
59             }
60              
61 0           return $rc;
62              
63             }
64              
65             sub error_handler {
66 0     0 1   my $self = shift;
67 0           my ($ex, $alias) = validate_params(\@_, [
68             1,
69             { optional => 1, default => undef },
70             ]);
71              
72 0           my $msg;
73 0           my ($errors, $rc) = $self->parse_exception($ex);
74              
75 0           $msg = $errors;
76 0 0         $msg = $self->message('exception', $alias, $errors) if (defined($alias));
77              
78 0           $self->log->error($msg);
79              
80             }
81              
82             sub parse_exception {
83 0     0 0   my $self= shift;
84 0           my ($ex) = validate_params(\@_, [1]);
85              
86 0           my $rc = 0;
87 0           my $errors;
88 0           my $ref = ref($ex);
89              
90 0 0         if ($ref) {
91              
92 0 0         if ($ex->isa('Badger::Exception')) {
93              
94 0           my $type = $ex->type;
95 0           my $info = compress($ex->info);
96              
97 0 0         if ($ex->match_type('dbix.class')) {
    0          
98              
99 0 0         if ($info =~ m/(.*) XAS::Database::Model::dbix_exception/) {
100              
101 0           $rc = 1;
102 0           $info = $1; # strip off the dbix stack dump
103            
104             }
105              
106             } elsif ($ex->match_type('xas.lib.app.signal_handler')) {
107            
108 0           die $ex; # propagate to the next level of error handlers
109              
110             }
111              
112 0 0         if ($ex->type =~ /pidfile\./) {
113              
114 0           $rc = 2;
115              
116             }
117              
118 0           $errors = $self->message('exception', $type, $info);
119              
120             } else {
121              
122 0           $rc = 1;
123 0           $errors = $self->message('unexpected', compress($ex));
124              
125             }
126              
127             } else {
128              
129 0           $rc = 1;
130 0           $errors = $self->message('unknownerror', compress($ex));
131              
132             }
133              
134 0           return $errors, $rc;
135              
136             }
137              
138             # ----------------------------------------------------------------------
139             # Private Methods
140             # ----------------------------------------------------------------------
141              
142             1;
143              
144             __END__
145              
146             =head1 NAME
147              
148             XAS::Lib::Mixin::Handlers - A mixin to provide exception handlers.
149              
150             =head1 SYNOPSIS
151              
152             use XAS::Class
153             version => '0.01',
154             base => 'XAS::Base',
155             mixin => 'XAS::Lib::Mixin::Handlers'
156             ;
157              
158              
159             =head1 DESCRIPTION
160              
161             This module provides exception handlers. It is implemented as a mixin.
162              
163             =head1 METHODS
164              
165             =head2 error_handler($ex)
166              
167             This method will write an 'error' entry to the current log. It takes
168             these parameters:
169              
170             =over 4
171              
172             =item B<$ex>
173              
174             The exception to handle.
175              
176             =back
177              
178             =head2 exception_hander($ex)
179              
180             The method will write an 'error' entry to the current log and send an
181             alert. It takes these parameters:
182              
183             =over 4
184              
185             =item B<$ex>
186              
187             The exception to handle.
188              
189             =back
190              
191             =head2 exit_handler($ex)
192              
193             The method will write an 'fatal' entry to the current log, send an
194             alert and return an exit code. It takes these parameters:
195              
196             =over 4
197              
198             =item B<$ex>
199              
200             The exception to handle.
201              
202             =back
203              
204             =head1 SEE ALSO
205              
206             =over 4
207              
208             =item L<XAS|XAS>
209              
210             =back
211              
212             =head1 AUTHOR
213              
214             Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
215              
216             =head1 COPYRIGHT AND LICENSE
217              
218             Copyright (C) 2014 Kevin L. Esteb
219              
220             This is free software; you can redistribute it and/or modify it under
221             the terms of the Artistic License 2.0. For details, see the full text
222             of the license at http://www.perlfoundation.org/artistic_license_2_0.
223              
224             =cut