| 1 | ############################################################################### | ||
| 2 | # XML::Template::Exception | ||
| 3 | # | ||
| 4 | # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@bbl.med.upenn.edu> | ||
| 5 | # All rights reserved. | ||
| 6 | # | ||
| 7 | # This program is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | ############################################################################### | ||
| 10 | package XML::Template::Exception; | ||
| 11 | |||
| 12 | |||
| 13 | use strict; | ||
| 14 | |||
| 15 | |||
| 16 | =pod | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | XML::Template::Exception - XML::Template exception handling class. | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | use XML::Template::Exception; | ||
| 25 | |||
| 26 | my $exception = XML::Template::Exception->new ('ERRORTYPE', | ||
| 27 | 'This is the error info.'); | ||
| 28 | my $type = $exception->type; | ||
| 29 | my $info = $exception->info; | ||
| 30 | if ($exception->isa ('DB')) { | ||
| 31 | ... | ||
| 32 | } | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | This modules provides a class for handing exceptions. Typically | ||
| 37 | exceptions are raised in code generated by Element modules. For instance, | ||
| 38 | here is some exception handling code from the DB Element module: | ||
| 39 | |||
| 40 | \$result = \$db->select (Table => \$tables, | ||
| 41 | Where => \$where); | ||
| 42 | die XML::Template::Exception->new ('DB', \$db->error ()) | ||
| 43 | if defined \$db->error (); | ||
| 44 | |||
| 45 | =head1 CONSTRUCTOR | ||
| 46 | |||
| 47 | The constructor takes two parameters. The first is the error type which a | ||
| 48 | short identifier. The type may contain dotted components (e.g. 'foo', | ||
| 49 | 'foo.bar', 'foo.bar.baz'), to indicate a hierarchy of exception types. | ||
| 50 | That is, 'foo.bar' would be a specific type of the more general 'foo' | ||
| 51 | type. If no type is given, the type is set to 'Undefined'. The second | ||
| 52 | parameter is some text that describes the exception. | ||
| 53 | |||
| 54 | The constructor returns a blessed array containg the exception type and | ||
| 55 | info. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub new { | ||
| 60 | my $proto = shift; | ||
| 61 | my ($type, $info) = @_; | ||
| 62 | |||
| 63 | $type = 'Undefined' if ! defined $type; | ||
| 64 | |||
| 65 | my $class = ref ($proto) || $proto; | ||
| 66 | return bless ([$type, $info], $class); | ||
| 67 | } | ||
| 68 | |||
| 69 | =head2 isa | ||
| 70 | |||
| 71 | if ($exception->isa ('foo.bar')) { | ||
| 72 | ... | ||
| 73 | } | ||
| 74 | |||
| 75 | This method returns 1 if the Exception is of the given type. The given | ||
| 76 | type is matched against each full type in the type hierarchy as well as | ||
| 77 | each child in the hierarchy. So, for instance, if the exception type is | ||
| 78 | C<foo.bar>, C<$exception->isa ('foo.bar')>, C<$exception->isa ('foo')>, | ||
| 79 | and C<$exception->isa ('bar')> will all return 1. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub isa { | ||
| 84 | my $self = shift; | ||
| 85 | my $type = shift; | ||
| 86 | |||
| 87 | my $etype = $self->[0]; | ||
| 88 | while ($etype) { | ||
| 89 | $etype =~ /([^\.]*$)/; | ||
| 90 | return 1 if $type eq $etype || $type eq $1; | ||
| 91 | |||
| 92 | $etype =~ s/\.?[^\.]*$//; | ||
| 93 | } | ||
| 94 | |||
| 95 | return 0; | ||
| 96 | } | ||
| 97 | |||
| 98 | =head2 type | ||
| 99 | |||
| 100 | my $type = $exception->type; | ||
| 101 | |||
| 102 | This method returns the exception type. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub type { | ||
| 107 | my $self = shift; | ||
| 108 | |||
| 109 | return $self->[0]; | ||
| 110 | } | ||
| 111 | |||
| 112 | =head2 info | ||
| 113 | |||
| 114 | my $info = $exception->info; | ||
| 115 | |||
| 116 | This method returns the info associated with the exception. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub info { | ||
| 121 | my $self = shift; | ||
| 122 | |||
| 123 | return $self->[1]; | ||
| 124 | } | ||
| 125 | |||
| 126 | =pod | ||
| 127 | |||
| 128 | =head1 AUTHOR | ||
| 129 | |||
| 130 | Jonathan Waxman | ||
| 131 | <jowaxman@bbl.med.upenn.edu> | ||
| 132 | |||
| 133 | =head1 COPYRIGHT | ||
| 134 | |||
| 135 | Copyright (c) 2002-2003 Jonathan A. Waxman | ||
| 136 | All rights reserved. | ||
| 137 | |||
| 138 | This program is free software; you can redistribute it and/or | ||
| 139 | modify it under the same terms as Perl itself. | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | |||
| 144 | 1; |