Use error handler
The error handler is registered as an application component named errorHandler. It can be configured in the application configuration as follows:
return [ 'components' => [ 'errorHandler' => [ 'maxSourceLines' => 20, ], ], ];
Custom error display
error handlerThe error handler adjusts the error display according to the value of the constant YII_DEBUG. When YII_DEBUG is true (indicating that it is in debug mode), the error handler will display the exception and the detailed function call stack and source The number of lines of code to help debugging. When YII_DEBUG is false, only error messages will be displayed to prevent the leakage of sensitive information of the application.
(Recommended tutorial: yii framework)
Information: If the exception inherits yii\base\UserException, regardless of the value of YII_DEBUG, the function call stack information will will not be displayed because this error will be considered a user-generated error and does not need to be corrected by the developer.
error handler The error handler uses two views to display errors by default:
1. @yii/views/errorHandler/error.php: displays errors that do not include function call stack information Information is used for all errors when YII_DEBUG is false.
2. @yii/views/errorHandler/exception.php: Used when displaying error messages containing function call stack information.
You can configure the errorView and exceptionView properties of the error handler to use a custom error display view.
Use error action
It is more convenient to use the specified error action to customize the error display. To do this, first configure the errorAction attribute of the errorHandler component, similar to the following:
return [ 'components' => [ 'errorHandler' => [ 'errorAction' => 'site/error', ], ] ];
More For programming-related content, please pay attention to the Introduction to Programming column on the php Chinese website!
The above is the detailed content of How to output error message in yii2. For more information, please follow other related articles on the PHP Chinese website!