20.11.2014 Views

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

eturn array(<br />

......<br />

'components'=>array(<br />

'errorHandler'=>array(<br />

'errorAction'=>'site/error',<br />

),<br />

),<br />

);<br />

In the above, we configure the CErrorHandler::errorAction property to be the route site/<br />

error which refers to the error action in SiteController. We may use a different route if<br />

needed.<br />

We can write the error action like the following:<br />

public function actionError()<br />

{<br />

if($error=<strong>Yii</strong>::app()->errorHandler->error)<br />

$this->render('error', $error);<br />

}<br />

In the action, we first retrieve the detailed error information from CErrorHandler::error. If it<br />

is not empty, we render the error view together with the error information. The error<br />

information returned from CErrorHandler::error is an array with the following fields:<br />

• code: the HTTP status code (e.g. 403, 500);<br />

• type: the error type (e.g. CHttpException, PHP Error);<br />

• message: the error message;<br />

• file: the name of the PHP script file where the error occurs;<br />

• line: the line number of the code where the error occurs;<br />

• trace: the call stack of the error;<br />

• source: the context source code where the error occurs.<br />

Tip: The reason we check if CErrorHandler::error is empty or not is because the error<br />

action may be directly requested by an end user, in which case there is no error. Since<br />

we are passing the$error array to the view, it will be automatically expanded to<br />

individual variables. As a result, in the view we can access directly the variables such<br />

as $code, $type.<br />

Message Logging<br />

A message of level error will always be logged when an error occurs. If the error is caused<br />

by a PHP warning or notice, the message will be logged with category php; if the error is<br />

caused by an uncaught exception, the category would be exception.ExceptionClassName<br />

(for CHttpException its statusCode will also be appended to the category). One can thus<br />

exploit the logging feature to monitor errors happened during application execution.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!