File Coverage

blib/lib/RSH/Exception.pm
Criterion Covered Total %
statement 55 170 32.3
branch 17 74 22.9
condition n/a
subroutine 9 22 40.9
pod 0 6 0.0
total 81 272 29.7


line stmt bran cond sub pod time code
1             # ------------------------------------------------------------------------------
2             # Copyright © 2003 by Matt Luker. All rights reserved.
3             #
4             # Revision:
5             #
6             # $Header$
7             #
8             # ------------------------------------------------------------------------------
9              
10             # Exception.pm - provides a flexible Exception object to allow C++/Java like
11             # exception handling syntax, containing all the main exception classes for
12             # RSH projects.
13             #
14             # Granted, you could just use "die STRING", but it isn't very flexible and it
15             # doesn't provide any kind of "typing". Which means you have to then rely on
16             # the string content if you want to do any kind of selective catch/throw
17             # logic. And that just isn't a good idea.
18             #
19             # This is a very simple object, basically providing type and data values. The
20             # catch method allows C++ syntax in perl code.
21             #
22             # @author Matt Luker
23             # @version $Revision: 3250 $
24              
25             # Exception.pm - provides a flexible Exception object to allow C++/Java like
26             # exception handling syntax, containing all the main exception classes for
27             # RSH projects.
28             #
29             # Copyright (C) 2003, Matt Luker
30             #
31             # This library is free software; you can redistribute it and/or modify
32             # it under the same terms as Perl itself.
33              
34             # If you have any questions about this software,
35             # or need to report a bug, please contact me.
36             #
37             # Matt Luker
38             # Port Angeles, WA
39             # kostya@redstarhackers.com
40             #
41             # TTGOG
42              
43             package RSH::Exception;
44              
45 4     4   55765 use 5.008;
  4         15  
  4         154  
46 4     4   23 use strict;
  4         5  
  4         144  
47 4     4   19 use warnings;
  4         16  
  4         330  
48              
49             require Exporter;
50              
51             our @ISA = qw(Exporter);
52              
53             # Items to export into callers namespace by default. Note: do not export
54             # names by default without a very good reason. Use EXPORT_OK instead.
55             # Do not simply export all your public functions/methods/constants.
56              
57             our @EXPORT_OK = qw(
58             );
59              
60             our @EXPORT = qw(
61             &catch
62             );
63              
64 4     4   32 use Carp;
  4         5  
  4         846  
65             use overload
66 4         44 'cmp' => \&compare,
67 4     4   5727 '""' => \&string;
  4         4602  
68              
69              
70             # ******************** PUBLIC Class Methods ********************
71              
72             # catch
73             #
74             # Allows syntax close to C++/Java's catch(type) syntax
75             #
76             # params:
77             # exception_type - the name of the exception type or "" for any type
78             # exception - the exception
79             #
80             # returns:
81             # 1 if the exception matches the type (or is defined if type is ""), 0 otherwise
82             #
83             sub catch {
84 4     4 0 297 my $exception_type = shift;
85 4         8 my $exception = shift;
86 4         5 my $block = shift;
87              
88 4 50       11 if (not defined($exception_type)) { die "Syntax Error: you must supply the exception type or \"\" for any type."; }
  0         0  
89              
90 4         6 my $match = 0;
91              
92 4 50       9 if (not defined($exception)) { $match = 0; }
  0         0  
93              
94 4 100       11 if (length($exception_type) == 0) {
95 1         3 $match = defined($exception);
96             } else {
97 3         14 $match = UNIVERSAL::isa($exception, $exception_type);
98             }
99            
100 4 100       8 if (not defined($block)) { return $match; }
  3         9  
101             else {
102 1 50       4 if ($match) { &$block($exception) }
  1         3  
103 1         249 return $match;
104             }
105             }
106              
107             # ******************** CONSTRUCTOR Methods ********************
108              
109             sub new {
110 10     10 0 1611 my $class = shift;
111              
112 10         37 my %params = @_;
113              
114 10         27 my $self = {};
115              
116 10         37 $self->{error_code} = $params{error_code};
117 10         18 $self->{message} = $params{message};
118              
119 10         33 bless $self, $class;
120              
121 10         39 return $self;
122             }
123              
124             # ******************** PUBLIC Instance Methods ********************
125              
126             # ******************** Accessor Methods ********************
127              
128             # error_code
129             #
130             # Read/Write accessor for error_code attribute
131             #
132             # params:
133             # $value - (optional) new error_code value
134             #
135             # returns:
136             # value of error_code attribute
137             #
138             sub error_code {
139 0     0 0 0 my $self = shift;
140 0         0 my $value = shift;
141              
142 0 0       0 if (defined($value)) {
143 0         0 $self->{error_code} = $value;
144             }
145            
146 0         0 return $self->{error_code};
147             }
148              
149             # message
150             #
151             # Read/Write accessor for message attribute
152             #
153             # params:
154             # $value - (optional) new message value
155             #
156             # returns:
157             # value of message attribute
158             #
159             sub message {
160 0     0 0 0 my $self = shift;
161 0         0 my $value = shift;
162              
163 0 0       0 if (defined($value)) {
164 0         0 $self->{message} = "". $value;
165             }
166              
167 0         0 return $self->{message};
168             }
169              
170             # ******************** Operator Overloading ********************
171              
172             # compare
173             #
174             # Performs eq test.
175             #
176             sub compare {
177 0     0 0 0 my $self = shift;
178 0         0 my $val = shift;
179 0         0 my $reversed = shift;
180              
181 0 0       0 if ($reversed eq '') { $reversed = 0; }
  0         0  
182              
183 0 0       0 if (UNIVERSAL::isa($val, 'RSH::Exception')) {
184             # there really isn't any way to compare exceptions
185 0 0       0 if (not $reversed) {
186 0         0 return ($self->string cmp $val->string);
187             } else {
188 0         0 return ($val->string cmp $self->string);
189             }
190             } else {
191 0 0       0 if (not $reversed) {
192 0         0 return ($self->string cmp $val);
193             } else {
194 0         0 return ($val cmp $self->string);
195             }
196             }
197             }
198              
199             # string
200             #
201             # Returns a string representation of the Exception object.
202             #
203             sub string {
204 16     16 0 10893 my $self = shift;
205              
206 16         487 my $string = "";
207              
208 16 100       63 if (defined($self->{error_code})) {
209 6         13 $string .= $self->{error_code};
210             }
211 16 100       44 if (defined($self->{message})) {
212 12 100       33 if (length($string) > 0) {
213 4         7 $string .= ": ";
214             }
215 12         27 $string .= $self->{message};
216             }
217              
218 16 100       39 if (length($string) == 0) { $string = "Exception!"; }
  2         3  
219              
220 16         4529 return Carp::longmess($string);
221             }
222              
223             # #################### Exception.pm ENDS ####################
224              
225             # CodeException.pm - exception class for code exceptions and errors.
226             #
227             # Examples would be parameter checking and handling.
228             #
229             # @author Matt Luker
230              
231             package RSH::CodeException;
232              
233             our @ISA = qw(Exporter RSH::Exception);
234              
235             # Items to export into callers namespace by default. Note: do not export
236             # names by default without a very good reason. Use EXPORT_OK instead.
237             # Do not simply export all your public functions/methods/constants.
238              
239             our @EXPORT_OK = qw(
240             );
241              
242             our @EXPORT = qw(
243             );
244              
245             # ******************** PUBLIC Class Methods ********************
246              
247              
248             # ******************** CONSTRUCTOR Methods ********************
249              
250             sub new {
251 0     0   0 my $class = shift;
252              
253 0         0 my %params = @_;
254              
255 0 0       0 if (not defined($params{error_code})) { $params{error_code} = 'RSH00500'; }
  0         0  
256 0 0       0 if (not defined($params{message})) { $params{message} = 'Code exception'; }
  0         0  
257              
258 0         0 my $self = new RSH::Exception %params;
259              
260 0         0 bless $self, $class;
261              
262 0         0 return $self;
263             }
264              
265             # ******************** PUBLIC Instance Methods ********************
266              
267             # #################### CodeException.pm ENDS ####################
268              
269             # NotImplementedException.pm - exception class for code exceptions and errors.
270             #
271             # Examples would be parameter checking and handling.
272             #
273             # @author Matt Luker
274              
275             package RSH::NotImplementedException;
276              
277             our @ISA = qw(Exporter RSH::Exception);
278              
279             # Items to export into callers namespace by default. Note: do not export
280             # names by default without a very good reason. Use EXPORT_OK instead.
281             # Do not simply export all your public functions/methods/constants.
282              
283             our @EXPORT_OK = qw(
284             );
285              
286             our @EXPORT = qw(
287             );
288              
289             # ******************** PUBLIC Class Methods ********************
290              
291              
292             # ******************** CONSTRUCTOR Methods ********************
293              
294             sub new {
295 0     0   0 my $class = shift;
296              
297 0         0 my %params = @_;
298              
299 0 0       0 if (not defined($params{error_code})) { $params{error_code} = 'RSH00510'; }
  0         0  
300 0 0       0 if (not defined($params{message})) { $params{message} = 'Code exception'; }
  0         0  
301              
302 0         0 my $self = new RSH::CodeException %params;
303              
304 0         0 bless $self, $class;
305              
306 0         0 return $self;
307             }
308              
309             # ******************** PUBLIC Instance Methods ********************
310              
311             # #################### NotImplementedException.pm ENDS ####################
312              
313             # SystemException.pm - exception class for system exceptions and errors.
314             #
315             # SystemExceptions should generally not be caught, or if caught,
316             # the application should fail with extreme prejudice.
317             #
318             # @author Matt Luker
319              
320             package RSH::SystemException;
321              
322             our @ISA = qw(Exporter RSH::Exception);
323              
324             # Items to export into callers namespace by default. Note: do not export
325             # names by default without a very good reason. Use EXPORT_OK instead.
326             # Do not simply export all your public functions/methods/constants.
327              
328             our @EXPORT_OK = qw(
329             );
330              
331             our @EXPORT = qw(
332             );
333              
334             # ******************** PUBLIC Class Methods ********************
335              
336              
337             # ******************** CONSTRUCTOR Methods ********************
338              
339             sub new {
340 0     0   0 my $class = shift;
341              
342 0         0 my %params = @_;
343              
344 0 0       0 if (not defined($params{error_code})) { $params{error_code} = 'RSH00900'; }
  0         0  
345 0 0       0 if (not defined($params{message})) { $params{message} = 'System exception'; }
  0         0  
346              
347 0         0 my $self = new RSH::Exception %params;
348              
349 0         0 bless $self, $class;
350              
351 0         0 return $self;
352             }
353              
354             # ******************** PUBLIC Instance Methods ********************
355              
356             # #################### SystemException.pm ENDS ####################
357              
358             # DataIntegrityException.pm - exception class for dataintegrity exceptions and errors.
359             #
360             # @author Matt Luker
361              
362             package RSH::DataIntegrityException;
363              
364             our @ISA = qw(Exporter RSH::Exception);
365              
366             # Items to export into callers namespace by default. Note: do not export
367             # names by default without a very good reason. Use EXPORT_OK instead.
368             # Do not simply export all your public functions/methods/constants.
369              
370             our @EXPORT_OK = qw(
371             );
372              
373             our @EXPORT = qw(
374             );
375              
376             # ******************** PUBLIC Class Methods ********************
377              
378              
379             # ******************** CONSTRUCTOR Methods ********************
380              
381             sub new {
382 1     1   7 my $class = shift;
383              
384 1         17 my %params = @_;
385              
386 1 50       14 if (not defined($params{error_code})) { $params{error_code} = 'RSH01000'; }
  1         9  
387 1 50       6 if (not defined($params{message})) { $params{message} = 'DataIntegrity exception'; }
  0         0  
388              
389 1         23 my $self = new RSH::Exception %params;
390              
391 1         24 bless $self, $class;
392              
393 1         10 return $self;
394             }
395              
396             # ******************** PUBLIC Instance Methods ********************
397              
398             # #################### DataIntegrityException.pm ENDS ####################
399              
400             # ConstraintException.pm - exception class for constraint exceptions and errors.
401             #
402             # @author Matt Luker
403              
404             package RSH::ConstraintException;
405              
406             our @ISA = qw(Exporter RSH::DataIntegrityException);
407              
408             # Items to export into callers namespace by default. Note: do not export
409             # names by default without a very good reason. Use EXPORT_OK instead.
410             # Do not simply export all your public functions/methods/constants.
411              
412             our @EXPORT_OK = qw(
413             );
414              
415             our @EXPORT = qw(
416             );
417              
418             # ******************** PUBLIC Class Methods ********************
419              
420              
421             # ******************** CONSTRUCTOR Methods ********************
422              
423             sub new {
424 0     0     my $class = shift;
425              
426 0           my %params = @_;
427              
428 0 0         if (not defined($params{error_code})) { $params{error_code} = 'RSH01010'; }
  0            
429 0 0         if (not defined($params{message})) { $params{message} = 'Constraint exception'; }
  0            
430              
431 0           my $self = new RSH::DataIntegrityException %params;
432              
433 0           bless $self, $class;
434              
435 0           return $self;
436             }
437              
438             # ******************** PUBLIC Instance Methods ********************
439              
440             # #################### ConstraintException.pm ENDS ####################
441              
442             # SecurityException.pm - exception class for security exceptions and errors.
443             #
444             # @author Matt Luker
445              
446             package RSH::SecurityException;
447              
448             our @ISA = qw(Exporter RSH::Exception);
449              
450             # Items to export into callers namespace by default. Note: do not export
451             # names by default without a very good reason. Use EXPORT_OK instead.
452             # Do not simply export all your public functions/methods/constants.
453              
454             our @EXPORT_OK = qw(
455             );
456              
457             our @EXPORT = qw(
458             );
459              
460             # ******************** PUBLIC Class Methods ********************
461              
462              
463             # ******************** CONSTRUCTOR Methods ********************
464              
465             sub new {
466 0     0     my $class = shift;
467              
468 0           my %params = @_;
469              
470 0 0         if (not defined($params{error_code})) { $params{error_code} = 'RSH02000'; }
  0            
471 0 0         if (not defined($params{message})) { $params{message} = 'Security exception'; }
  0            
472              
473 0           my $self = new RSH::Exception %params;
474              
475 0           bless $self, $class;
476              
477 0           return $self;
478             }
479              
480             # ******************** PUBLIC Instance Methods ********************
481              
482             # #################### SecurityException.pm ENDS ####################
483              
484             # BadPasswordException.pm - exception class for bad password exceptions and errors.
485             #
486             # @author Matt Luker
487              
488             package RSH::BadPasswordException;
489              
490             our @ISA = qw(Exporter RSH::SecurityException);
491              
492             # Items to export into callers namespace by default. Note: do not export
493             # names by default without a very good reason. Use EXPORT_OK instead.
494             # Do not simply export all your public functions/methods/constants.
495              
496             our @EXPORT_OK = qw(
497             );
498              
499             our @EXPORT = qw(
500             );
501              
502             # ******************** PUBLIC Class Methods ********************
503              
504              
505             # ******************** CONSTRUCTOR Methods ********************
506              
507             sub new {
508 0     0     my $class = shift;
509              
510 0           my %params = @_;
511              
512 0 0         if (not defined($params{error_code})) { $params{error_code} = 'RSH02010'; }
  0            
513 0 0         if (not defined($params{message})) { $params{message} = 'Bad password exception'; }
  0            
514              
515 0           my $self = new RSH::SecurityException %params;
516              
517 0           bless $self, $class;
518              
519 0           return $self;
520             }
521              
522             # ******************** PUBLIC Instance Methods ********************
523              
524             # #################### BadPasswordException.pm ENDS ####################
525              
526             # FileException.pm - exception class for file exceptions and errors.
527             #
528             # Examples would be parameter checking and handling.
529             #
530             # @author Matt Luker
531              
532             package RSH::FileException;
533              
534             our @ISA = qw(Exporter RSH::Exception);
535              
536             # Items to export into callers namespace by default. Note: do not export
537             # names by default without a very good reason. Use EXPORT_OK instead.
538             # Do not simply export all your public functions/methods/constants.
539              
540             our @EXPORT_OK = qw(
541             );
542              
543             our @EXPORT = qw(
544             );
545              
546             # ******************** PUBLIC Class Methods ********************
547              
548              
549             # ******************** CONSTRUCTOR Methods ********************
550              
551             sub new {
552 0     0     my $class = shift;
553              
554 0           my %params = @_;
555              
556 0 0         if (not defined($params{error_code})) { $params{error_code} = 'RSH03000'; }
  0            
557 0 0         if (not defined($params{message})) { $params{message} = 'File Exception'; }
  0            
558              
559 0           my $self = new RSH::Exception %params;
560              
561 0           bless $self, $class;
562              
563 0           return $self;
564             }
565              
566             # ******************** PUBLIC Instance Methods ********************
567              
568             # #################### FileException.pm ENDS ####################
569              
570             # FileNotFoundException.pm - exception class for filenotfound exceptions and errors.
571             #
572             # Examples would be parameter checking and handling.
573             #
574             # @author Matt Luker
575              
576             package RSH::FileNotFoundException;
577              
578             our @ISA = qw(Exporter RSH::FileException);
579              
580             # Items to export into callers namespace by default. Note: do not export
581             # names by default without a very good reason. Use EXPORT_OK instead.
582             # Do not simply export all your public functions/methods/constants.
583              
584             our @EXPORT_OK = qw(
585             );
586              
587             our @EXPORT = qw(
588             );
589              
590             # ******************** PUBLIC Class Methods ********************
591              
592              
593             # ******************** CONSTRUCTOR Methods ********************
594              
595             sub new {
596 0     0     my $class = shift;
597              
598 0           my %params = @_;
599              
600 0 0         if (not defined($params{error_code})) { $params{error_code} = 'RSH03010'; }
  0            
601 0 0         if (not defined($params{message})) { $params{message} = 'File not found exception'; }
  0            
602              
603 0           my $self = new RSH::FileException %params;
604              
605 0           bless $self, $class;
606              
607 0           return $self;
608             }
609              
610             # ******************** PUBLIC Instance Methods ********************
611              
612             # #################### FileNotFoundException.pm ENDS ####################
613              
614             # RuntimeException.pm - exception class for runtime exceptions and errors.
615             #
616             # Examples would be parameter checking and handling.
617             #
618             # @author Matt Luker
619              
620             package RSH::RuntimeException;
621              
622             our @ISA = qw(Exporter RSH::Exception);
623              
624             # Items to export into callers namespace by default. Note: do not export
625             # names by default without a very good reason. Use EXPORT_OK instead.
626             # Do not simply export all your public functions/methods/constants.
627              
628             our @EXPORT_OK = qw(
629             );
630              
631             our @EXPORT = qw(
632             );
633              
634             # ******************** PUBLIC Class Methods ********************
635              
636              
637             # ******************** CONSTRUCTOR Methods ********************
638              
639             sub new {
640 0     0     my $class = shift;
641              
642 0           my %params = @_;
643              
644 0 0         if (not defined($params{error_code})) { $params{error_code} = 'RSH09000'; }
  0            
645 0 0         if (not defined($params{message})) { $params{message} = 'Runtime exception'; }
  0            
646              
647 0           my $self = new RSH::Exception %params;
648              
649 0           bless $self, $class;
650              
651 0           return $self;
652             }
653              
654             # ******************** PUBLIC Instance Methods ********************
655              
656             # #################### RuntimeException.pm ENDS ####################
657              
658             # IndexOutOfBoundsException.pm - exception class for indexOutOfBounds exceptions and errors.
659             #
660             # Examples would be parameter checking and handling.
661             #
662             # @author Matt Luker
663              
664             package RSH::IndexOutOfBoundsException;
665              
666             our @ISA = qw(Exporter RSH::Exception);
667              
668             # Items to export into callers namespace by default. Note: do not export
669             # names by default without a very good reason. Use EXPORT_OK instead.
670             # Do not simply export all your public functions/methods/constants.
671              
672             our @EXPORT_OK = qw(
673             );
674              
675             our @EXPORT = qw(
676             );
677              
678             # ******************** PUBLIC Class Methods ********************
679              
680              
681             # ******************** CONSTRUCTOR Methods ********************
682              
683             sub new {
684 0     0     my $class = shift;
685              
686 0           my %params = @_;
687              
688 0 0         if (not defined($params{error_code})) { $params{error_code} = 'RSH09010'; }
  0            
689 0 0         if (not defined($params{message})) { $params{message} = 'Index out of bounds exception'; }
  0            
690              
691 0           my $self = new RSH::Exception %params;
692              
693 0           bless $self, $class;
694              
695 0           return $self;
696             }
697              
698             # ******************** PUBLIC Instance Methods ********************
699              
700             # #################### IndexOutOfBoundsException.pm ENDS ####################
701              
702             1;
703              
704             __END__
705              
706             # TTGOG
707              
708             # ------------------------------------------------------------------------------
709             #
710             # $Log$
711             # Revision 1.5 2004/04/09 06:18:26 kostya
712             # Added quote escaping capabilities.
713             #
714             # Revision 1.4 2003/12/27 07:41:04 kostya
715             # Spelling error ;-)
716             #
717             # Revision 1.3 2003/11/14 05:26:58 kostya
718             # Added some new exception types.
719             #
720             # Revision 1.2 2003/10/15 01:07:00 kostya
721             # documentation and license updates--everything is Artistic.
722             #
723             # Revision 1.1.1.1 2003/10/13 01:38:04 kostya
724             # First import
725             #
726             # Revision 1.4 2003/08/23 07:11:43 kostya
727             # New file exceptions.
728             #
729             # Revision 1.3 2003/08/23 01:00:33 kostya
730             # Added a lot of exceptions and set up some inheritance.
731             #
732             # Revision 1.2 2003/08/01 06:12:54 kostya
733             # Changed ConstraintException package name for less typing ;-)
734             #
735             # Revision 1.1 2003/07/29 20:35:02 kostya
736             # Exception class.
737             #
738             #
739             # ------------------------------------------------------------------------------
740