File Coverage

File:blib/lib/OpenSRF/EX.pm
Coverage:56.3%

linestmtbrancondsubpodtimecode
1package OpenSRF::EX;
2
13
13
13
91
53
103
use Error qw(:try);
3
13
13
13
92
53
109
use base qw( OpenSRF Error );
4
13
13
13
104
50
90
use OpenSRF::Utils::Logger;
5
6my $log = "OpenSRF::Utils::Logger";
7$Error::Debug = 1;
8
9sub new {
10
0
1
        my( $class, $message ) = @_;
11
0
        $class = ref( $class ) || $class;
12
0
        my $self = {};
13
0
0
        $self->{'msg'} = ${$class . '::ex_msg_header'} .": $message";
14
0
        return bless( $self, $class );
15}
16
17
0
0
sub message() { return $_[0]->{'msg'}; }
18
19
0
sub DESTROY{}
20
21
22 - 28
=head1 OpenSRF::EX

Top level exception.  This class logs an exception when it is thrown.  Exception subclasses
should subclass one of OpenSRF::EX::INFO, NOTICE, WARN, ERROR, CRITICAL, and PANIC and provide
a new() method that takes a message and a message() method that returns that message.

=cut
29
30 - 43
=head2 Synopsis


	throw OpenSRF::EX::Jabber ("I Am Dying");

	OpenSRF::EX::InvalidArg->throw( "Another way" );

	my $je = OpenSRF::EX::Jabber->new( "I Cannot Connect" );
	$je->throw();


	See OpenSRF/EX.pm for example subclasses.

=cut
44
45# Log myself and throw myself
46
47#sub message() { shift->alert_abstract(); }
48
49#sub new() { shift->alert_abstract(); }
50
51sub throw() {
52
53
0
1
        my $self = shift;
54
55
0
        if( ! ref( $self ) || scalar( @_ ) ) {
56
0
                $self = $self->new( @_ );
57        }
58
59
0
        if( $self->class->isa( "OpenSRF::EX::INFO" ) ||
60                                $self->class->isa( "OpenSRF::EX::NOTICE" ) ||
61                                $self->class->isa( "OpenSRF::EX::WARN" ) ) {
62
63
0
                $log->debug( $self->stringify(), $log->DEBUG );
64        }
65
66
0
        else{ $log->debug( $self->stringify(), $log->ERROR ); }
67
68
0
        $self->SUPER::throw;
69}
70
71
72sub stringify() {
73
0
1
        my $self = shift;
74
0
        my($package, $file, $line) = get_caller();
75
0
        my $name = ref($self);
76
0
        my $msg = $self->message();
77
78
0
    my ($sec,$min,$hour,$mday,$mon,$year) = localtime();
79
0
0
    $year += 1900; $mon += 1;
80
0
    my $date = sprintf(
81        '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d',
82        $year, $mon, $mday, $hour, $min, $sec);
83
84
0
    return "Exception: $name $date $package $file:$line $msg\n";
85}
86
87
88# --- determine the originating caller of this exception
89sub get_caller() {
90
91
0
0
        my $package = caller();
92
0
        my $x = 0;
93
0
        while( $package->isa( "Error" ) || $package =~ /^Error::/ ) {
94
0
                $package = caller( ++$x );
95        }
96
0
        return (caller($x));
97}
98
99
100
101
102# -------------------------------------------------------------------
103# -------------------------------------------------------------------
104
105# Top level exception subclasses defining the different exception
106# levels.
107
108# -------------------------------------------------------------------
109
110package OpenSRF::EX::INFO;
111
13
13
13
107
46
78
use base qw(OpenSRF::EX);
112our $ex_msg_header = "System INFO";
113
114# -------------------------------------------------------------------
115
116package OpenSRF::EX::NOTICE;
117
13
13
13
94
52
73
use base qw(OpenSRF::EX);
118our $ex_msg_header = "System NOTICE";
119
120# -------------------------------------------------------------------
121
122package OpenSRF::EX::WARN;
123
13
13
13
88
52
79
use base qw(OpenSRF::EX);
124our $ex_msg_header = "System WARNING";
125
126# -------------------------------------------------------------------
127
128package OpenSRF::EX::ERROR;
129
13
13
13
92
57
78
use base qw(OpenSRF::EX);
130our $ex_msg_header = "System ERROR";
131
132# -------------------------------------------------------------------
133
134package OpenSRF::EX::CRITICAL;
135
13
13
13
88
58
74
use base qw(OpenSRF::EX);
136our $ex_msg_header = "System CRITICAL";
137
138# -------------------------------------------------------------------
139
140package OpenSRF::EX::PANIC;
141
13
13
13
86
52
72
use base qw(OpenSRF::EX);
142our $ex_msg_header = "System PANIC";
143
144# -------------------------------------------------------------------
145# -------------------------------------------------------------------
146
147# Some basic exceptions
148
149# -------------------------------------------------------------------
150package OpenSRF::EX::Jabber;
151
13
13
13
95
54
73
use base 'OpenSRF::EX::ERROR';
152our $ex_msg_header = "Jabber Exception";
153
154package OpenSRF::EX::JabberDisconnected;
155
13
13
13
88
43
70
use base 'OpenSRF::EX::ERROR';
156our $ex_msg_header = "JabberDisconnected Exception";
157
158 - 162
=head2 OpenSRF::EX::Jabber

Thrown when there is a problem using the Jabber service

=cut
163
164package OpenSRF::EX::Transport;
165
13
13
13
92
50
72
use base 'OpenSRF::EX::ERROR';
166our $ex_msg_header = "Transport Exception";
167
168
169
170# -------------------------------------------------------------------
171package OpenSRF::EX::InvalidArg;
172
13
13
13
98
53
68
use base 'OpenSRF::EX::ERROR';
173our $ex_msg_header = "Invalid Arg Exception";
174
175 - 179
=head2 OpenSRF::EX::InvalidArg

Thrown where an argument to a method was invalid or not provided

=cut
180
181
182# -------------------------------------------------------------------
183package OpenSRF::EX::Socket;
184
13
13
13
93
54
70
use base 'OpenSRF::EX::ERROR';
185our $ex_msg_header = "Socket Exception";
186
187 - 191
=head2 OpenSRF::EX::Socket

Thrown when there is a network layer exception

=cut
192
193
194
195# -------------------------------------------------------------------
196package OpenSRF::EX::Config;
197
13
13
13
89
48
75
use base 'OpenSRF::EX::PANIC';
198our $ex_msg_header = "Config Exception";
199
200 - 205
=head2 OpenSRF::EX::Config

Thrown when a package requires a config option that it cannot retrieve
or the config file itself cannot be loaded

=cut
206
207
208# -------------------------------------------------------------------
209package OpenSRF::EX::User;
210
13
13
13
92
55
70
use base 'OpenSRF::EX::ERROR';
211our $ex_msg_header = "User Exception";
212
213 - 217
=head2 OpenSRF::EX::User

Thrown when an error occurs due to user identification information

=cut
218
219package OpenSRF::EX::Session;
220
13
13
13
91
54
71
use base 'OpenSRF::EX::ERROR';
221our $ex_msg_header = "Session Error";
222
223
2241;