Triggering Errors

In several of the methods described in this document it is possible to trigger an error.

For example, if you were implementing the "page publish rules" driver used as an example earlier in this book, you might want to trigger a "404 Not Found" error if a user tries to access a page that is no longer published.

You can trigger an error simply by throwing an exception. You can throw either the standard PHP Exception, or you can throw any custom exceptions you may have previously defined.

In any case, the desired HTTP error should be set in the exception. This is achieved with the Exception by specifying it as the second argument to the constructor. For example, to trigger a "404 Not Found" error you would use code such as the following: throw new Exception('Page not found', 404).

Recite will use the built-in error-handling mechanism for handling thrown exceptions. For instance, if you throw a 404 error then Recite will render the error template as chosen by the site administrator in the Recite Control Panel.

Note

If the code thrown in the exception is not a valid HTTP code then Recite will send a 500 code with the response.

Each method that supports exceptions has it noted in its description in this document.

Caution

In some cases when you throw an exception from one of the specified methods the handleBootException() method in your driver (and all other render hooks) will subsequently be executed.

Conversely, if another render hook triggers an exception then this will also result in your handleBootException() method being called.