TAL error-handling

The TAL on-error attribute provides a mechanism to handle errors in rendering, avoiding a complete failure to render a page. A parallel in many programming languages is a try/catch construct.

Syntax

tal:on-error="MODE EXPRESSION"

The MODE & EXPRESSION parts of an on-error statement work identically to a tal:content attribute. All that differs is the method of invocation.

Invoking an error handler

If an unhandled error is encountered whilst rendering a page with TAL, an on-error attribute is searched in the current element (where the error was raised). If it is found then it is used, treated like a TAL content attribute. If no on-error attribute is found then the parent element is searched, and so on moving upwards through the hierarchy through the element's ancestors.

In the example below, if an error was raised whilst evaluating the TALES expression Model/CurrentUser/Name then the entire paragraph would be replaced with There was a problem determining your log-in status. If, on the other hand, that evaluated successfully but there was en error whilst evaluating Model/CurrentUser/GetProfileUrl then the hyperlink text would be replaced with Error! Cannot get profile URL but the paragraph itself would still be rendered.

<p tal:on-error="string:There was a problem determining your log-in status">
  You are logged in as
  <span tal:content="Model/CurrentUser/Name">Joseph Bloggs</span>.<br>
  <a tal:on-error="string:Error! Cannot get profile URL"
     tal:attributes="href Model/CurrentUser/GetProfileUrl">Your profile</a>
</p>