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