1 #============================================================= -*-Perl-*-
6 # Module implementing a generic exception class used for error handling
7 # in the Template Toolkit.
10 # Andy Wardley <abw@wardley.org>
13 # Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
18 #========================================================================
20 package Template::Exception;
24 use constant TYPE => 0;
25 use constant INFO => 1;
26 use constant TEXT => 2;
27 use overload q|""| => "as_string", fallback => 1;
32 #------------------------------------------------------------------------
33 # new($type, $info, \$text)
35 # Constructor method used to instantiate a new Template::Exception
36 # object. The first parameter should contain the exception type. This
37 # can be any arbitrary string of the caller's choice to represent a
38 # specific exception. The second parameter should contain any
39 # information (i.e. error message or data reference) relevant to the
40 # specific exception event. The third optional parameter may be a
41 # reference to a scalar containing output text from the template
42 # block up to the point where the exception was thrown.
43 #------------------------------------------------------------------------
46 my ($class, $type, $info, $textref) = @_;
47 bless [ $type, $info, $textref ], $class;
51 #------------------------------------------------------------------------
56 # Accessor methods to return the internal TYPE and INFO fields.
57 #------------------------------------------------------------------------
72 #------------------------------------------------------------------------
76 # Method to return the text referenced by the TEXT member. A text
77 # reference may be passed as a parameter to supercede the existing
78 # member. The existing text is added to the *end* of the new text
79 # before being stored. This facility is provided for template blocks
80 # to gracefully de-nest when an exception occurs and allows them to
81 # reconstruct their output in the correct order.
82 #------------------------------------------------------------------------
85 my ($self, $newtextref) = @_;
86 my $textref = $self->[ TEXT ];
89 $$newtextref .= $$textref if $textref && $textref ne $newtextref;
90 $self->[ TEXT ] = $newtextref;
102 #------------------------------------------------------------------------
105 # Accessor method to return a string indicating the exception type and
107 #------------------------------------------------------------------------
111 return $self->[ TYPE ] . ' error - ' . $self->[ INFO ];
115 #------------------------------------------------------------------------
116 # select_handler(@types)
118 # Selects the most appropriate handler for the exception TYPE, from
119 # the list of types passed in as parameters. The method returns the
120 # item which is an exact match for TYPE or the closest, more
121 # generic handler (e.g. foo being more generic than foo.bar, etc.)
122 #------------------------------------------------------------------------
125 my ($self, @options) = @_;
126 my $type = $self->[ TYPE ];
128 @hlut{ @options } = (1) x @options;
131 return $type if $hlut{ $type };
133 # strip .element from the end of the exception type to find a
134 # more generic handler
135 $type =~ s/\.?[^\.]*$//;
146 Template::Exception - Exception handling class module
150 use Template::Exception;
152 my $exception = Template::Exception->new($type, $info);
153 $type = $exception->type;
154 $info = $exception->info;
155 ($type, $info) = $exception->type_info;
157 print $exception->as_string();
159 $handler = $exception->select_handler(\@candidates);
163 The C<Template::Exception> module defines an object class for
164 representing exceptions within the template processing life cycle.
165 Exceptions can be raised by modules within the Template Toolkit, or
166 can be generated and returned by user code bound to template
169 Exceptions can be raised in a template using the C<THROW> directive,
171 [% THROW user.login 'no user id: please login' %]
173 or by calling the L<throw()|Template::Context#throw()> method on the current
174 L<Template::Context> object,
176 $context->throw('user.passwd', 'Incorrect Password');
177 $context->throw('Incorrect Password'); # type 'undef'
179 or from Perl code by calling C<die()> with a C<Template::Exception> object,
181 die (Template::Exception->new('user.denied', 'Invalid User ID'));
183 or by simply calling C<die()> with an error string. This is
184 automagically caught and converted to an exception of 'C<undef>'
185 type (that's the literal string 'C<undef>' rather than Perl's
186 undefined value) which can then be handled in the usual way.
188 die "I'm sorry Dave, I can't do that";
190 Each exception is defined by its type and a information component
191 (e.g. error message). The type can be any identifying string and may
192 contain dotted components (e.g. 'C<foo>', 'C<foo.bar>', 'C<foo.bar.baz>').
193 Exception types are considered to be hierarchical such that 'C<foo.bar>'
194 would be a specific type of the more general 'C<foo>' type.
200 Returns the exception type.
204 Returns the exception information.
208 Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/>
212 Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
214 This module is free software; you can redistribute it and/or
215 modify it under the same terms as Perl itself.
219 L<Template>, L<Template::Context>
225 # perl-indent-level: 4
226 # indent-tabs-mode: nil
229 # vim: expandtab shiftwidth=4: