PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/guide/topics.error.txt

https://bitbucket.org/21h/torchok-anon
Plain Text | 159 lines | 129 code | 30 blank | 0 comment | 0 complexity | 55b083e305779431bd365fe33bf7df06 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, BSD-2-Clause
  1. Error Handling
  2. ==============
  3. Yii provides a complete error handling framework based on the PHP 5
  4. exception mechanism. When the application is created to handle an incoming
  5. user request, it registers its [handleError|CApplication::handleError]
  6. method to handle PHP warnings and notices; and it registers its
  7. [handleException|CApplication::handleException] method to handle uncaught
  8. PHP exceptions. Consequently, if a PHP warning/notice or an uncaught
  9. exception occurs during the application execution, one of the error
  10. handlers will take over the control and start the necessary error handling
  11. procedure.
  12. > Tip: The registration of error handlers is done in the application's
  13. constructor by calling PHP functions
  14. [set_exception_handler](http://www.php.net/manual/en/function.set-exception-handler.php)
  15. and [set_error_handler](http://www.php.net/manual/en/function.set-error-handler.php).
  16. If you do not want Yii to handle the errors and exceptions, you may define
  17. constant `YII_ENABLE_ERROR_HANDLER` and `YII_ENABLE_EXCEPTION_HANDLER` to
  18. be false in the [entry script](/doc/guide/basics.entry).
  19. By default, [errorHandler|CApplication::errorHandler] (or
  20. [exceptionHandler|CApplication::exceptionHandler]) will raise an
  21. [onError|CApplication::onError] event (or
  22. [onException|CApplication::onException] event). If the error (or exception)
  23. is not handled by any event handler, it will call for help from the
  24. [errorHandler|CErrorHandler] application component.
  25. Raising Exceptions
  26. ------------------
  27. Raising exceptions in Yii is not different from raising a normal PHP
  28. exception. One uses the following syntax to raise an exception when needed:
  29. ~~~
  30. [php]
  31. throw new ExceptionClass('ExceptionMessage');
  32. ~~~
  33. Yii defines two exception classes: [CException] and [CHttpException]. The
  34. former is a generic exception class, while the latter represents an
  35. exception that should be displayed to end users. The latter also carries a
  36. [statusCode|CHttpException::statusCode] property representing an HTTP
  37. status code. The class of an exception determines how it should be
  38. displayed, as we will explain next.
  39. > Tip: Raising a [CHttpException] exception is a simple way of reporting
  40. errors caused by user misoperation. For example, if the user provides an
  41. invalid post ID in the URL, we can simply do the following to show a 404
  42. error (page not found):
  43. ~~~
  44. [php]
  45. // if post ID is invalid
  46. throw new CHttpException(404,'The specified post cannot be found.');
  47. ~~~
  48. Displaying Errors
  49. -----------------
  50. When an error is forwarded to the [CErrorHandler] application component,
  51. it chooses an appropriate view to display the error. If the error is meant
  52. to be displayed to end users, such as a [CHttpException], it will use a
  53. view named `errorXXX`, where `XXX` stands for the HTTP status code (e.g.
  54. 400, 404, 500). If the error is an internal one and should only be
  55. displayed to developers, it will use a view named `exception`. In the
  56. latter case, complete call stack as well as the error line information will
  57. be displayed.
  58. > Info: When the application runs in [production
  59. mode](/doc/guide/basics.entry#debug-mode), all errors including those internal
  60. ones will be displayed using view `errorXXX`. This is because the call
  61. stack of an error may contain sensitive information. In this case,
  62. developers should rely on the error logs to determine what is the real
  63. cause of an error.
  64. [CErrorHandler] searches for the view file corresponding to a view in the
  65. following order:
  66. 1. `WebRoot/themes/ThemeName/views/system`: this is the `system` view
  67. directory under the currently active theme.
  68. 2. `WebRoot/protected/views/system`: this is the default `system` view
  69. directory for an application.
  70. 3. `yii/framework/views`: this is the standard system view directory
  71. provided by the Yii framework.
  72. Therefore, if we want to customize the error display, we can simply create
  73. error view files under the system view directory of our application or
  74. theme. Each view file is a normal PHP script consisting of mainly HTML
  75. code. For more details, please refer to the default view files under the
  76. framework's `view` directory.
  77. Handling Errors Using an Action
  78. -------------------------------
  79. Starting from version 1.0.6, Yii allows using a [controller action](/doc/guide/basics.controller#action)
  80. to handle the error display work. To do so, we should configure the error handler
  81. in the application configuration as follows:
  82. ~~~
  83. [php]
  84. return array(
  85. ......
  86. 'components'=>array(
  87. 'errorHandler'=>array(
  88. 'errorAction'=>'site/error',
  89. ),
  90. ),
  91. );
  92. ~~~
  93. In the above, we configure the [CErrorHandler::errorAction] property to be the route
  94. `site/error` which refers to the `error` action in `SiteController`. We may use a different
  95. route if needed.
  96. We can write the `error` action like the following:
  97. ~~~
  98. [php]
  99. public function actionError()
  100. {
  101. if($error=Yii::app()->errorHandler->error)
  102. $this->render('error', $error);
  103. }
  104. ~~~
  105. In the action, we first retrieve the detailed error information from [CErrorHandler::error].
  106. If it is not empty, we render the `error` view together with the error information.
  107. The error information returned from [CErrorHandler::error] is an array with the following fields:
  108. * `code`: the HTTP status code (e.g. 403, 500);
  109. * `type`: the error type (e.g. [CHttpException], `PHP Error`);
  110. * `message`: the error message;
  111. * `file`: the name of the PHP script file where the error occurs;
  112. * `line`: the line number of the code where the error occurs;
  113. * `trace`: the call stack of the error;
  114. * `source`: the context source code where the error occurs.
  115. > Tip: The reason we check if [CErrorHandler::error] is empty or not is because
  116. the `error` action may be directly requested by an end user, in which case there is no error.
  117. Since we are passing the `$error` array to the view, it will be automatically expanded
  118. to individual variables. As a result, in the view we can access directly the variables such as
  119. `$code`, `$type`.
  120. Message Logging
  121. ---------------
  122. A message of level `error` will always be logged when an error occurs. If
  123. the error is caused by a PHP warning or notice, the message will be logged
  124. with category `php`; if the error is caused by an uncaught exception, the
  125. category would be `exception.ExceptionClassName` (for [CHttpException] its
  126. [statusCode|CHttpException::statusCode] will also be appended to the
  127. category). One can thus exploit the [logging](/doc/guide/topics.logging)
  128. feature to monitor errors happened during application execution.
  129. <div class="revision">$Id: topics.error.txt 2739 2010-12-14 01:50:04Z weizhuo $</div>