File: Error.pm

package info (click to toggle)
libgd-graph-perl 1.33-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 480 kB
  • ctags: 188
  • sloc: perl: 3,677; makefile: 83
file content (346 lines) | stat: -rw-r--r-- 8,810 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#==========================================================================
#			   Copyright (c) 1995-2000 Martien Verbruggen
#--------------------------------------------------------------------------
#
#	Name:
#		GD::Graph::Error.pm
#
# $Id: Error.pm,v 1.5 2000/10/07 05:52:41 mgjv Exp $
#
#==========================================================================

package GD::Graph::Error;

$GD::Graph::Error::VERSION = '$Revision: 1.5 $' =~ /\s([\d.]+)/;

use strict;
use Carp;

my %Errors;
use vars qw( $Debug $ErrorLevel $CriticalLevel );

$Debug = 0;

# Warnings from 0 to 4, Errors from 5 to 9, and Critical 10 and above.
$ErrorLevel    = 5;
$CriticalLevel = 10;

=head1 NAME

GD::Graph::Error - Error handling for GD::Graph classes

=head1 SYNOPSIS

use GD::Graph::Error_subclass;

=head1 DESCRIPTION

This class is a parent for all GD::Graph classes, including
GD::Graph::Data, and offers error and warning handling and some
debugging control.

Errors are stored in a lexical hash in this package, so the
implementation of the subclass should be irrelevant. 

=head1 PUBLIC METHODS

These methods can be used by users of any of the subclasses of
GD::Graph::Error to get at the errors of objects or classes.

=head2 $object->error() OR Class->error()

Returns a list of all the errors that the current object has
accumulated. In scalar context, returns the last error. If called as a
class method it works at a class level. This is handy when a constructor
fails, for example:

  my $data = GD::Graph::Data->new()    
      or die GD::Graph::Data->error;
  $data->read(file => '/foo/bar.data') 
      or die $data->error;

or if you really are only interested in the last error:

  $data->read(file => '/foo/bar.data') 
      or die scalar $data->error;

This implementation does not clear the error list, so if you don't die
on errors, you will need to make sure to never ask for anything but the
last error (put this in scalar context) or to call C<clear_error()> now
and again.

Errors are more verbose about where the errors originated if the
$GD::Graph::Error::Debug variable is set to a true value, and even more
verbose if this value is larger than 5.

If $Debug is larger than 3, both of these will always return the
full list of errors and warnings (although the meaning of C<has_warning>
and C<has_error> does not change).

=cut

sub _error
{
	my $self = shift;
	my $min_level = shift || 0;
	my $max_level = shift || 1 << 31;
	return unless exists $Errors{$self};
	my $error = $Errors{$self};

	my @return;

	@return = 
		map { 
			($Debug > 3 ? "[$_->{level}] " : '') .
			"$_->{msg}" .
			($Debug ? " at $_->{whence}[1] line $_->{whence}[2]" : '') .
			($Debug > 5 ? " => $_->{caller}[0]($_->{caller}[2])" : '') .
			"\n"
		} 
		grep { $_->{level} >= $min_level && $_->{level} <= $max_level }
		@$error;

	wantarray && @return > 1 and  
		$return[-1] =~ s/\n/\n\t/ or
		$return[-1] =~ s/\n//;

	return wantarray ? @return : $return[-1];
}

sub error
{
	my $self = shift;
	$Debug > 3 and return $self->_error();
	$self->_error($ErrorLevel);
}

sub warning
{
	my $self = shift;
	$Debug > 3 and return $self->_error();
	$self->_error(0, $ErrorLevel - 1);
}

=head2 $object->has_error() OR Class->has_error()

=head2 $object->has_warning() OR Class->has_warning()

Returns true if there are pending errors (warnings) for the object
(or class). To be more precise, it returns a list of errors in list
context, and the number of errors in scalar context.

This allows you to check for errors and warnings after a large number of
operations which each might fail:

  $data->read(file => '/foo/bar.data') or die $data->error;
  while (my @foo = $sth->fetchrow_array)
  {
      $data->add_point(@foo);
  }
  $data->set_x(12, 'Foo');
  $data->has_warning and warn $data->warning;
  $data->has_error   and die  $data->error;

The reason to call this, instead of just calling C<error()> or
C<warning()> and looking at its return value, is that this method is
much more efficient and fast.

If you want to count anything as bad, just set $ErrorLevel to 0, after
which you only need to call C<has_error>.

=cut

sub has_error
{
	my $self = shift;
	return unless exists $Errors{$self};
	grep { $_->{level} >= $ErrorLevel } @{$Errors{$self}};
}

sub has_warning
{
	my $self = shift;
	return unless exists $Errors{$self};
	grep { $_->{level} < $ErrorLevel } @{$Errors{$self}};
}

=head2 $object->clear_errors() or Class->clear_errors()

Clears all outstanding errors.

=cut

sub clear_errors
{
	my $self = shift;
	delete $Errors{$self};
}

=head1 PROTECTED METHODS

These methods are only to be called from within this class and its
Subclasses.

=head2 $object->_set_error(I<arg>) or Class->_set_error(I<arg>)

=head2 $object->_set_warning(I<arg>) or Class->_set_warning(I<arg>)

Subclasses call this to set an error. The argument can be a reference
to an array, of which the first element should be the error level, and
the second element the error message. Alternatively, it can just be the
message, in which case the error level will be assumed to be
$ErrorLevel.

If the error level is >= $CriticalLevel the program will die, using
Carp::croak to display the current message, as well as all the other
error messages pending.

In the current implementation these are almost identical when called
with a scalar argument, except that the default ewrror level is
different. When called with an array reference, they are identical in
function. This may change in the future. They're mainly here for code
clarity.

=cut

# Private, for construction of error hash. This should probably be an
# object, but that's too much work right now.
sub __error_hash
{
	my $caller  = shift;
	my $default = shift;
	my $msg     = shift;

	my %error = (caller => $caller);

	if (ref($msg) && ref($msg) eq 'ARRAY' && @{$msg} >= 2)
	{
		# Array reference
		$error{level} = $msg->[0];
		$error{msg}   = $msg->[1];
	}
	elsif (ref($_[0]) eq '')
	{
		# simple scalar
		$error{level} = $default;
		$error{msg}   = $msg;
	}
	else
	{
		# someting else, which I can't deal with
		warn "Did you read the documentation for GD::Graph::Error?";
		return;
	}

	my $lvl = 1;
	while (my @c = caller($lvl))
	{
		$error{whence} = [@c[0..2]];
		$lvl++;
	}

	return \%error;
}

sub _set_error
{
	my $self = shift;
	return unless @_;

	while (@_)
	{
		my $e_h = __error_hash([caller], $ErrorLevel, shift) or return;
		push @{$Errors{$self}}, $e_h;
		croak $self->error if $e_h->{level} >= $CriticalLevel;
	}
	return;
}

sub _set_warning
{
	my $self = shift;
	return unless @_;

	while (@_)
	{
		my $e_h = __error_hash([caller], $ErrorLevel, shift) or return;
		push @{$Errors{$self}}, $e_h;
		croak $self->error if $e_h->{level} >= $CriticalLevel;
	}
	return;
}

=head2 $object->_move_errors

Move errors from an object into the class it belongs to.  This can be
useful if something nasty happens in the constructor, while
instantiating one of these objects, and you need to move these errors
into the class space before returning. (see GD::Graph::Data::new for an
example)

=cut

sub _move_errors
{
	my $self = shift;
	my $class = ref($self);
	push @{$Errors{$class}}, @{$Errors{$self}};
	return;
}

sub _dump
{
	my $self = shift;
	require Data::Dumper;
	my $dd = Data::Dumper->new([$self], ['me']);
	$dd->Dumpxs;
}

=head1 VARIABLES

=head2 $GD::Graph::Error::Debug

The higher this value, the more verbose error messages will be. At the
moment, any true value will cause the line number and source file of the
caller at the top of the stack to be included, a value of more than 2
will include the error severity, and a value of more than 5 will also
include the direct caller's (i.e. the spot where the error message was
generated) line number and package. Default: 0.

=head2 $GD::Graph::Error::ErrorLevel

Errors levels below this value will be counted as warnings, and error
levels above (and inclusive) up to $CriticalLevel will be counted as
errors. This is also the default error level for the C<_set_error()>
method. This value should be 0 or larger, and smaller than
$CriticalLevel. Default: 5.

=head2 $GD::Graph::Error::CriticalLevel

Any errorlevel of or above this level will immediately cause the program
to die with the specified message, using Carp::croak. Default: 10.

=head1 NOTES

As with all Modules for Perl: Please stick to using the interface. If
you try to fiddle too much with knowledge of the internals of this
module, you could get burned. I may change them at any time.

=head1 AUTHOR

Martien Verbruggen E<lt>mgjv@tradingpost.com.auE<gt>

=head2 Copyright

Copyright (c) 2000 Martien Verbruggen.

All rights reserved. This package is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

=head1 SEE ALSO

L<GD::Graph>, L<GD::Graph::Data>

=cut

"Just another true value";