Adding custom error representations
Tonic will return all sorts of HTTP error response codes if somethin ggoes wrong, but often you want to add a nice representation to go with them. Here's a nice simple way to do so.
Once we've loaded and executed our resource, we're left with a response object in our dispatcher. From here, we can check for an error response code and load another representation if needed. The following amends to the dispatcher will load another representation if the requested resource returns a 404 error:
// generate response
$response =& $request->exec($adapter, $resource);
// handle errors
switch ($response->statusCode) {
case '404':
$resource =& Resource::find($adapter, '/404.html');
$response =& $resource->get($request);
if ($response->success()) {
$response->statusCode = 404;
}
break;
}
// get representation if there is one
if ($resource && $representation =& $resource->loadRepresentation($adapter)) {
$response =& $representation->get($request);
}
// output response
$response->output();
dispatch.php
This can obviously be extended to include other response error codes.
Back to documentation homeCreated Jan 28, 2008, last modified Jan 28, 2008