1 |
## ------------------------------------------------------------------------ |
2 |
## $Id: Exception.pm,v 1.2 2003/02/20 20:53:55 joko Exp $ |
3 |
## ------------------------------------------------------------------------ |
4 |
## $Log: Exception.pm,v $ |
5 |
## Revision 1.2 2003/02/20 20:53:55 joko |
6 |
## + fix: using Object's basic log method if not running together with the Logger |
7 |
## |
8 |
## Revision 1.1 2003/02/19 00:35:09 joko |
9 |
## + initial commit |
10 |
## |
11 |
## ------------------------------------------------------------------------ |
12 |
|
13 |
|
14 |
package DesignPattern::Exception; |
15 |
|
16 |
use strict; |
17 |
use warnings; |
18 |
|
19 |
|
20 |
use base qw( DesignPattern::Object ); |
21 |
|
22 |
|
23 |
sub checkExceptions { |
24 |
my $self = shift; |
25 |
|
26 |
# behaviour A: die on error |
27 |
#die ($@) if $@; |
28 |
|
29 |
# behaviour B: log the error message as a warning |
30 |
if ($@) { |
31 |
if ($self->can('log')) { |
32 |
$self->log($@, 'warning'); |
33 |
} else { |
34 |
#print "warning: ", $@, "\n"; |
35 |
#print "warning: ", $@, "\n"; |
36 |
$self->log_basic($@, 'warning'); |
37 |
} |
38 |
return 1; |
39 |
} |
40 |
|
41 |
} |
42 |
|
43 |
sub raiseException { |
44 |
my $self = shift; |
45 |
my $message = shift; |
46 |
my $level = shift; |
47 |
$message ||= 'UNKNOWN ERROR'; |
48 |
$level ||= 'critical'; |
49 |
$self->_viaLogger($message, $level); |
50 |
} |
51 |
|
52 |
sub _viaLogger { |
53 |
my $self = shift; |
54 |
my $message = shift; |
55 |
my $level = shift; |
56 |
# get logger instance |
57 |
my $logger = eval { Log::Dispatch::Config->instance; }; |
58 |
if ($logger) { |
59 |
$logger->log( message => $message, level => $level ); |
60 |
} else { |
61 |
print "ERROR[$level] $message", "\n"; |
62 |
} |
63 |
} |
64 |
|
65 |
1; |
66 |
__END__ |