[Templates] Error Handling

Andy Wardley abw@cre.canon.co.uk
Tue, 15 Aug 2000 14:47:10 +0100


On Aug 14,  9:17pm, Jonas Liljegren wrote:
>  The Question
>  ============
>
> And here it comes:  How would you do this?

I've read through the above, but not studied it in detail, so let
me know if I miss an important point.

I would do it something like this:

    # action taken before processing template
    if ($action) {
	if ($everything_ok) {
	    # just the result
	    $result = $the_result_value;
	}
	else {
	    # a closure to throw the exception
	    $result = sub {
		die Template::Exception->new('barf', $the_error);
	    };
	}
    }

    ...

    my $template = Template->new(
	ERROR => 'error.html',
    );

    my $replace = {
        result => $result,
	...
    }

    $template->process($handler, $replace);

In the template, you simply access the variable:

    [% result %]

This will either return a value or throw an error.  If an error is thrown
then the 'error.html' template will get processed instead.

You can also do a local test for failure if you want to catch the error,
but continue processing the same template.

    [% TRY %]
       [% result %]
    [% CATCH barf %]
       <b>An error occured: [% error.info %]</b>
    [% END %]

Does that help or is there something more complex that I'm failing to
account for?

> (Just writing this gave me some new ideas, but I should not make this
> any longer now. ;-)

Feel free.  I'm listening...


A

-- 
Andy Wardley <abw@kfs.org>   Signature regenerating.  Please remain seated.
     <abw@cre.canon.co.uk>   For a good time: http://www.kfs.org/~abw/