Yii includes a built-in error handler which makes error handling a much more pleasant
experience than before. In particular, the Yii error handler does the following to improve error handling:
- All non-fatal PHP errors (e.g. warnings, notices) are converted into catchable exceptions.
- Exceptions and fatal PHP errors are displayed with detailed call stack information and source code lines
in debug mode. - Supports using a dedicated controller action to display errors.
- Supports different error response formats.
The error handler is enabled by default. You may disable it by defining the constant
YII_ENABLE_ERROR_HANDLER to be false in the entry script of your application.
Using Error Handler ¶
The error handler is registered as an application component named errorHandler.
You may configure it in the application configuration like the following:
return [
'components' => [
'errorHandler' => [
'maxSourceLines' => 20,
],
],
];
With the above configuration, the number of source code lines to be displayed in exception pages will be up to 20.
As aforementioned, the error handler turns all non-fatal PHP errors into catchable exceptions. This means you can
use the following code to deal with PHP errors:
use Yii;
use yiibaseErrorException;
try {
10/0;
} catch (ErrorException $e) {
Yii::warning("Division by zero.");
}
// execution continues...
If you want to show an error page telling the user that his request is invalid or unexpected, you may simply
throw an HTTP exception, such as yiiwebNotFoundHttpException. The error handler
will correctly set the HTTP status code of the response and use an appropriate error view to display the error
message.
use yiiwebNotFoundHttpException;
throw new NotFoundHttpException();
Customizing Error Display ¶
The error handler adjusts the error display according to the value of the constant YII_DEBUG.
When YII_DEBUG is true (meaning in debug mode), the error handler will display exceptions with detailed call
stack information and source code lines to help easier debugging. And when YII_DEBUG is false, only the error
message will be displayed to prevent revealing sensitive information about the application.
Info: If an exception is a descendant of yiibaseUserException, no call stack will be displayed regardless
the value ofYII_DEBUG. This is because such exceptions are considered to be caused by user mistakes and the
developers do not need to fix anything.
By default, the error handler displays errors using two views:
@yii/views/errorHandler/error.php: used when errors should be displayed WITHOUT call stack information.
WhenYII_DEBUGisfalse, this is the only error view to be displayed.@yii/views/errorHandler/exception.php: used when errors should be displayed WITH call stack information.
You can configure the errorView and exceptionView
properties of the error handler to use your own views to customize the error display.
Using Error Actions ¶
A better way of customizing the error display is to use dedicated error actions.
To do so, first configure the errorAction property of the errorHandler
component like the following:
return [
'components' => [
'errorHandler' => [
'errorAction' => 'site/error',
],
]
];
The errorAction property takes a route
to an action. The above configuration states that when an error needs to be displayed without call stack information,
the site/error action should be executed.
You can create the site/error action as follows,
namespace appcontrollers;
use Yii;
use yiiwebController;
class SiteController extends Controller
{
public function actions()
{
return [
'error' => [
'class' => 'yiiwebErrorAction',
],
];
}
}
The above code defines the error action using the yiiwebErrorAction class which renders an error
using a view named error.
Besides using yiiwebErrorAction, you may also define the error action using an action method like the following,
public function actionError()
{
$exception = Yii::$app->errorHandler->exception;
if ($exception !== null) {
return $this->render('error', ['exception' => $exception]);
}
}
You should now create a view file located at views/site/error.php. In this view file, you can access
the following variables if the error action is defined as yiiwebErrorAction:
name: the name of the error;message: the error message;exception: the exception object through which you can retrieve more useful information, such as HTTP status code,
error code, error call stack, etc.
Info: If you are using the basic project template or the advanced project template,
the error action and the error view are already defined for you.
Note: If you need to redirect in an error handler, do it the following way:
Yii::$app->getResponse()->redirect($url)->send(); return;
Customizing Error Response Format ¶
The error handler displays errors according to the format setting of the response.
If the response format is html, it will use the error or exception view
to display errors, as described in the last subsection. For other response formats, the error handler will
assign the array representation of the exception to the yiiwebResponse::$data property which will then
be converted to different formats accordingly. For example, if the response format is json, you may see
the following response:
HTTP/1.1 404 Not Found
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
{
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"code": 0,
"status": 404
}
You may customize the error response format by responding to the beforeSend event of the response component
in the application configuration:
return [
// ...
'components' => [
'response' => [
'class' => 'yiiwebResponse',
'on beforeSend' => function ($event) {
$response = $event->sender;
if ($response->data !== null) {
$response->data = [
'success' => $response->isSuccessful,
'data' => $response->data,
];
$response->statusCode = 200;
}
},
],
],
];
The above code will reformat the error response like the following:
HTTP/1.1 200 OK
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
{
"success": false,
"data": {
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"code": 0,
"status": 404
}
}
Обработка ошибок ¶
В состав Yii входит встроенный обработчик ошибок, делающий работу с ошибками гораздо более
приятным занятием. А именно:
- Все не фатальные ошибки PHP (то есть warning, notice) конвертируются в исключения, которые можно перехватывать.
- Исключения и фатальные ошибки PHP отображаются в режиме отладки с детальным стеком вызовов и исходным кодом.
- Можно использовать для отображения ошибок действие контроллера.
- Поддерживаются различные форматы ответа.
По умолчанию обработчик ошибок включен. Вы можете выключить его объявив константу
YII_ENABLE_ERROR_HANDLER со значением false во входном скрипте вашего приложения.
Использование обработчика ошибок ¶
Обработчик ошибок регистрируется в качестве компонента приложения
с именем errorHandler. Вы можете настраивать его следующим образом:
return [
'components' => [
'errorHandler' => [
'maxSourceLines' => 20,
],
],
];
С приведённой выше конфигурацией на странице ошибки будет отображаться до 20 строк исходного кода.
Как уже было упомянуто, обработчик ошибок конвертирует все не фатальные ошибки PHP в перехватываемые исключения.
Это означает что можно поступать с ошибками следующим образом:
use Yii;
use yiibaseErrorException;
try {
10/0;
} catch (ErrorException $e) {
Yii::warning("Деление на ноль.");
}
// можно продолжать выполнение
Если вам необходимо показать пользователю страницу с ошибкой, говорящей ему о том, что его запрос не верен или не
должен был быть сделан, вы можете выкинуть исключение HTTP, такое как
yiiwebNotFoundHttpException. Обработчик ошибок корректно выставит статус код HTTP для ответа и использует
подходящий вид страницы ошибки.
use yiiwebNotFoundHttpException;
throw new NotFoundHttpException();
Настройка отображения ошибок ¶
Обработчик ошибок меняет отображение ошибок в зависимости от значения константы YII_DEBUG.
При YII_DEBUG равной true (режим отладки), обработчик ошибок будет отображать для облегчения отладки детальный стек
вызовов и исходный код. При YII_DEBUG равной false отображается только сообщение об ошибке, тем самым не позволяя
получить информацию о внутренностях приложения.
Информация: Если исключение является наследником yiibaseUserException, стек вызовов не отображается вне
зависимости от значенияYII_DEBUGтак как такие исключения считаются ошибками пользователя и исправлять что-либо
разработчику не требуется.
По умолчанию обработчик ошибок показывает ошибки используя два представления:
@yii/views/errorHandler/error.php: используется для отображения ошибок БЕЗ стека вызовов.
ПриYII_DEBUGравнойfalseиспользуется только это преставление.@yii/views/errorHandler/exception.php: используется для отображения ошибок СО стеком вызовов.
Вы можете настроить свойства errorView и exceptionView
для того, чтобы использовать свои представления.
Использование действий для отображения ошибок ¶
Лучшим способом изменения отображения ошибок является использование действий путём
конфигурирования свойства errorAction компонента errorHandler:
// ...
'components' => [
// ...
'errorHandler' => [
'errorAction' => 'site/error',
],
]
Свойство errorAction принимает маршрут
действия. Конфигурация выше означает, что для отображения ошибки без стека вызовов будет использовано действие site/error.
Само действие можно реализовать следующим образом:
namespace appcontrollers;
use Yii;
use yiiwebController;
class SiteController extends Controller
{
public function actions()
{
return [
'error' => [
'class' => 'yiiwebErrorAction',
],
];
}
}
Приведённый выше код задаёт действие error используя класс yiiwebErrorAction, который рендерит ошибку используя
отображение error.
Вместо использования yiiwebErrorAction вы можете создать действие error как обычный метод:
public function actionError()
{
$exception = Yii::$app->errorHandler->exception;
if ($exception !== null) {
return $this->render('error', ['exception' => $exception]);
}
}
Вы должны создать файл представления views/site/error.php. В этом файле, если используется yiiwebErrorAction,
вам доступны следующие переменные:
name: имя ошибки;message: текст ошибки;exception: объект исключения, из которого можно получить дополнительную информацию, такую как статус HTTP,
код ошибки, стек вызовов и т.д.
Информация: Если вы используете шаблоны приложения basic или advanced,
действие error и файл представления уже созданы за вас.
Изменение формата ответа ¶
Обработчик ошибок отображает ошибки в соответствии с выбранным форматом ответа.
Если формат ответа задан как html, будут использованы представления для ошибок и
исключений, как описывалось ранее. Для остальных форматов ответа обработчик ошибок присваивает массив данных,
представляющий ошибку свойству yiiwebResponse::$data. Оно далее конвертируется в необходимый формат. Например,
если используется формат ответа json, вы получите подобный ответ:
HTTP/1.1 404 Not Found
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
{
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"code": 0,
"status": 404
}
Изменить формат можно в обработчике события beforeSend компонента response в конфигурации приложения:
return [
// ...
'components' => [
'response' => [
'class' => 'yiiwebResponse',
'on beforeSend' => function ($event) {
$response = $event->sender;
if ($response->data !== null) {
$response->data = [
'success' => $response->isSuccessful,
'data' => $response->data,
];
$response->statusCode = 200;
}
},
],
],
];
Приведённый код изменит формат ответа на подобный:
HTTP/1.1 200 OK
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
{
"success": false,
"data": {
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"code": 0,
"status": 404
}
}
Обработка ошибок
Если при обработке запроса к RESTful API в запросе пользователя обнаруживается ошибка или происходит
что-то непредвиденное на сервере, вы можете просто выбрасывать исключение, чтобы уведомить пользователя о нештатной ситуации.
Если вы можете установить конкретную причину ошибки (например, запрошенный ресурс не существует), вам следует подумать
о том, чтобы выбрасывать исключение с соответствующим кодом состояния HTTP (например, [[yiiwebNotFoundHttpException]],
соответствующее коду состояния 404). Yii отправит ответ с соответствующим
HTTP-кодом и текстом. Он также включит в тело ответа сериализованное представление
исключения. Например:
HTTP/1.1 404 Not Found
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
{
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"code": 0,
"status": 404
}
Сводный список кодов состояния HTTP, используемых REST-фреймворком Yii:
200: OK. Все сработало именно так, как и ожидалось.201: Ресурс был успешно создан в ответ наPOST-запрос. ЗаголовокLocation
содержит URL, указывающий на только что созданный ресурс.204: Запрос обработан успешно, и в ответе нет содержимого (для запросаDELETE, например).304: Ресурс не изменялся. Можно использовать закэшированную версию.400: Неверный запрос. Может быть связано с разнообразными проблемами на стороне пользователя, такими как неверные JSON-данные
в теле запроса, неправильные параметры действия, и т.д.401: Аутентификация завершилась неудачно.403: Аутентифицированному пользователю не разрешен доступ к указанной точке входа API.404: Запрошенный ресурс не существует.405: Метод не поддерживается. Сверьтесь со списком поддерживаемых HTTP-методов в заголовкеAllow.415: Не поддерживаемый тип данных. Запрашивается неправильный тип данных или номер версии.422: Проверка данных завершилась неудачно (в ответе наPOST-запрос, например). Подробные сообщения об ошибках смотрите в теле ответа.429: Слишком много запросов. Запрос отклонен из-за превышения ограничения частоты запросов.500: Внутренняя ошибка сервера. Возможная причина — ошибки в самой программе.
Свой формат ответа с ошибкой
Вам может понадобиться изменить формат ответа с ошибкой. Например, вместо использования разных статусов ответа HTTP
для разных ошибок, вы можете всегда отдавать статус 200, а реальный код статуса отдавать как часть JSON ответа:
HTTP/1.1 200 OK
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
{
"success": false,
"data": {
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"code": 0,
"status": 404
}
}
Для этого можно использовать событие beforeSend компонента response прямо в конфигурации приложения:
return [ // ... 'components' => [ 'response' => [ 'class' => 'yiiwebResponse', 'on beforeSend' => function ($event) { $response = $event->sender; if ($response->data !== null && !empty(Yii::$app->request->get('suppress_response_code'))) { $response->data = [ 'success' => $response->isSuccessful, 'data' => $response->data, ]; $response->statusCode = 200; } }, ], ], ];
Приведённый выше код изменит формат ответа (как для удачного запроса, так и для ошибок) если передан GET-параметр
suppress_response_code.
После установки Yii2 уже имеется файл view-шаблона views/site/error.php. Чтобы получить свою страницу ошибки, можно просто отредактировать этот файл. В нём доступны три переменные: $name, $message, $exception. Признак ошибки 404 — это значение свойства statusCode объекта $exception. Таким образом можно написать свою проверку этого значения и для разных ошибок показывать пользователю разный ответ.
Почему для показа сообщения об ошибках используется шаблон views/site/error.php? Это задается в настройках приложения, в файле config/web.php:
$config = [ /* ... */ 'components' => [ /* ... */ 'errorHandler' => [ 'errorAction' => 'site/error' ], /* ... */ ], /* ... */ ];
Для обработки ошибок будет использован метод actionError() класса SiteController:
class SiteController extends Controller { /* ... */ public function actionError() { $exception = Yii::$app->errorHandler->exception; if ($exception !== null) { return $this->render('error', ['exception' => $exception]); } } /* ... */ }
Но если мы заглянем в исходный код controllers/SiteController.php, то обнаружим, что такого метода там нет. А вместо него есть метод actions():
class SiteController extends Controller { /* ... */ public function actions() { return [ 'error' => [ // объявляем действие error и задаем имя класса 'class' => 'yiiwebErrorAction', ], ]; } /* ... */ }
Все действия контроллера можно разделить на встроенные и отдельные. Встроенные действия определены как методы контроллера, например actionError(). Отдельные действия указываются в карте действий контроллера, в методе actions(). Сам же функционал таких действий описан в отдельных классах.
Мы договорились ранее использовать класс SiteController только как образец. Давайте зададим свой обработчик ошибки в файле конфигурации config/web.php:
$config = [ /* ... */ 'components' => [ /* ... */ 'errorHandler' => [ 'errorAction' => 'app/error' ], /* ... */ ], /* ... */ ];
Добавим метод actions() в класс AppController:
class AppController extends Controller { /* ... */ public function actions() { return [ 'error' => [ 'class' => 'yiiwebErrorAction', ], ]; } /* ... */ }
Создадим view-шаблон views/app/error.php:
<?php /* @var $this yiiwebView */ /* @var $name string */ /* @var $message string */ /* @var $exception Exception */ use yiihelpersHtml; $this->title = $name; ?> <div class="container"> <h1><?= Html::encode($this->title) ?></h1> <div class="alert alert-danger"> <?= nl2br(Html::encode($message)) ?> </div> </div>
И внесем изменения в класс CatalogController, чтобы он выбрасывал исключение, когда в базе данных не удается найти категорию, бренд или товар каталога:
<?php namespace appcontrollers; use appmodelsCategory; use appmodelsBrand; use appmodelsProduct; use yiiwebHttpException; use Yii; class CatalogController extends AppController { /** * Главная страница каталога товаров */ public function actionIndex() { // получаем корневые категории $roots = Yii::$app->cache->get('root-categories'); if ($roots === false) { $roots = Category::find()->where(['parent_id' => 0])->asArray()->all(); Yii::$app->cache->set('root-categories', $roots); } // получаем популярные бренды $brands = Yii::$app->cache->get('popular-brands'); if ($brands === false) { $brands = (new Brand())->getPopularBrands(); Yii::$app->cache->set('popular-brands', $brands); } return $this->render('index', compact('roots', 'brands')); } /** * Категория каталога товаров */ public function actionCategory($id, $page = 1) { $id = (int)$id; $page = (int)$page; // пробуем извлечь данные из кеша $data = Yii::$app->cache->get('category-'.$id.'-page-'.$page); if ($data === null) { // данные есть в кеше, но такой категории не существует throw new HttpException( 404, 'Запрошенная страница не найдена' ); } if ($data === false) { // данных нет в кеше, получаем их заново $temp = new Category(); // данные о категории $category = $temp->getCategory($id); if (!empty($category)) { // такая категория существует // товары категории list($products, $pages) = $temp->getCategoryProducts($id); // сохраняем полученные данные в кеше $data = [$products, $pages, $category]; Yii::$app->cache->set('category-' . $id . '-page-' . $page, $data); } else { // такая категория не существует Yii::$app->cache->set('category-' . $id . '-page-' . $page, null); throw new HttpException( 404, 'Запрошенная страница не найдена' ); } } list($products, $pages, $category) = $data; // устанавливаем мета-теги для страницы $this->setMetaTags( $category['name'] . ' | ' . Yii::$app->params['shopName'], $category['keywords'], $category['description'] ); return $this->render( 'category', compact('category', 'products', 'pages') ); } /** * Список всех брендов каталога товаров */ public function actionBrands() { // пробуем извлечь данные из кеша $brands = Yii::$app->cache->get('all-brands'); if ($brands === false) { // данных нет в кеше, получаем их заново $brands = (new Brand())->getAllBrands(); // сохраняем полученные данные в кеше Yii::$app->cache->set('all-brands', $brands); } return $this->render( 'brands', compact('brands') ); } /** * Список товаров бренда с идентификатором $id */ public function actionBrand($id, $page = 1) { $id = (int)$id; $page = (int)$page; // пробуем извлечь данные из кеша $data = Yii::$app->cache->get('brand-'.$id.'-page-'.$page); if ($data === null) { // данные есть в кеше, но такого бренда не существует throw new HttpException( 404, 'Запрошенная страница не найдена' ); } if ($data === false) { // данных нет в кеше, получаем их заново $temp = new Brand(); // данные о бренде $brand = $temp->getBrand($id); if (!empty($brand)) { // такой бренд существует // товары бренда list($products, $pages) = $temp->getBrandProducts($id); // сохраняем полученные данные в кеше $data = [$products, $pages, $brand]; Yii::$app->cache->set('brand-'.$id.'-page-'.$page, $data); } else { // такой бренд не существует Yii::$app->cache->set('brand-'.$id.'-page-'.$page, null); throw new HttpException( 404, 'Запрошенная страница не найдена' ); } } list($products, $pages, $brand) = $data; // устанавливаем мета-теги $this->setMetaTags( $brand['name'] . ' | ' . Yii::$app->params['shopName'], $brand['keywords'], $brand['description'] ); return $this->render( 'brand', compact('brand', 'products', 'pages') ); } /** * Страница товара с идентификатором $id */ public function actionProduct($id) { $id = (int)$id; // пробуем извлечь данные из кеша $data = Yii::$app->cache->get('product-'.$id); if ($data === null) { // данные есть в кеше, но такого товара не существует throw new HttpException( 404, 'Запрошенная страница не найдена' ); } if ($data === false) { // данных нет в кеше, получаем их заново $product = (new Product())->getProduct($id); if (!empty($product)) { // такой товар существует $brand = (new Brand())->getBrand($product['brand_id']); $data = [$product, $brand]; // сохраняем полученные данные в кеше Yii::$app->cache->set('product-' . $id, $data); } else { // такого товара не существует Yii::$app->cache->set('product-' . $id, null); throw new HttpException( 404, 'Запрошенная страница не найдена' ); } } list($product, $brand) = $data; // устанавливаем мета-теги $this->setMetaTags( $product['name'] . ' | ' . Yii::$app->params['shopName'], $product['keywords'], $product['description'] ); // получаем популярные товары, похожие на текущий $similar = Yii::$app->cache->get('similar-'.$product['id']); if ($similar === false) { // товары из той же категории того же бренда $similar = Product::find() ->where([ 'hit' => 1, 'category_id' => $product['category_id'], 'brand_id' => $product['brand_id'] ]) ->andWhere(['NOT IN', 'id', $product['id']]) ->limit(3) ->asArray() ->all(); Yii::$app->cache->set('similar-'.$product['id'], $similar); } return $this->render( 'product', compact('product', 'brand', 'similar') ); } }
Поиск:
Web-разработка • Yii2 • Интернет магазин • Исключение • Каталог товаров • Практика • Фреймворк • 404 • Not Found • Exception • Ошибка • Error
Каталог оборудования
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Производители
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Функциональные группы
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Yii includes a built-in [[yiiwebErrorHandler|error handler]] which makes error handling a much more pleasant
experience than before. In particular, the Yii error handler does the following to improve error handling:
- All non-fatal PHP errors (e.g. warnings, notices) are converted into catchable exceptions.
- Exceptions and fatal PHP errors are displayed with detailed call stack information and source code lines
in debug mode. - Supports using a dedicated controller action to display errors.
- Supports different error response formats.
The [[yiiwebErrorHandler|error handler]] is enabled by default. You may disable it by defining the constant
YII_ENABLE_ERROR_HANDLER to be false in the entry script of your application.
Using Error Handler
The [[yiiwebErrorHandler|error handler]] is registered as an application component named errorHandler.
You may configure it in the application configuration like the following:
return [
'components' => [
'errorHandler' => [
'maxSourceLines' => 20,
],
],
];
With the above configuration, the number of source code lines to be displayed in exception pages will be up to 20.
As aforementioned, the error handler turns all non-fatal PHP errors into catchable exceptions. This means you can
use the following code to deal with PHP errors:
use Yii;
use yiibaseErrorException;
try {
10/0;
} catch (ErrorException $e) {
Yii::warning("Division by zero.");
}
// execution continues...
If you want to show an error page telling the user that his request is invalid or unexpected, you may simply
throw an [[yiiwebHttpException|HTTP exception]], such as [[yiiwebNotFoundHttpException]]. The error handler
will correctly set the HTTP status code of the response and use an appropriate error view to display the error
message.
use yiiwebNotFoundHttpException;
throw new NotFoundHttpException();
Customizing Error Display
The [[yiiwebErrorHandler|error handler]] adjusts the error display according to the value of the constant YII_DEBUG.
When YII_DEBUG is true (meaning in debug mode), the error handler will display exceptions with detailed call
stack information and source code lines to help easier debugging. And when YII_DEBUG is false, only the error
message will be displayed to prevent revealing sensitive information about the application.
Info: If an exception is a descendant of [[yiibaseUserException]], no call stack will be displayed regardless
the value ofYII_DEBUG. This is because such exceptions are considered to be caused by user mistakes and the
developers do not need to fix anything.
By default, the [[yiiwebErrorHandler|error handler]] displays errors using two views:
@yii/views/errorHandler/error.php: used when errors should be displayed WITHOUT call stack information.
WhenYII_DEBUGis false, this is the only error view to be displayed.@yii/views/errorHandler/exception.php: used when errors should be displayed WITH call stack information.
You can configure the [[yiiwebErrorHandler::errorView|errorView]] and [[yiiwebErrorHandler::exceptionView|exceptionView]]
properties of the error handler to use your own views to customize the error display.
Using Error Actions
A better way of customizing the error display is to use dedicated error actions.
To do so, first configure the [[yiiwebErrorHandler::errorAction|errorAction]] property of the errorHandler
component like the following:
return [
'components' => [
'errorHandler' => [
'errorAction' => 'site/error',
],
]
];
The [[yiiwebErrorHandler::errorAction|errorAction]] property takes a route
to an action. The above configuration states that when an error needs to be displayed without call stack information,
the site/error action should be executed.
You can create the site/error action as follows,
namespace appcontrollers;
use Yii;
use yiiwebController;
class SiteController extends Controller
{
public function actions()
{
return [
'error' => [
'class' => 'yiiwebErrorAction',
],
];
}
}
The above code defines the error action using the [[yiiwebErrorAction]] class which renders an error
using a view named error.
Besides using [[yiiwebErrorAction]], you may also define the error action using an action method like the following,
public function actionError()
{
$exception = Yii::$app->errorHandler->exception;
if ($exception !== null) {
return $this->render('error', ['exception' => $exception]);
}
}
You should now create a view file located at views/site/error.php. In this view file, you can access
the following variables if the error action is defined as [[yiiwebErrorAction]]:
name: the name of the error;message: the error message;exception: the exception object through which you can retrieve more useful information, such as HTTP status code,
error code, error call stack, etc.
Info: If you are using the basic project template or the advanced project template,
the error action and the error view are already defined for you.
Customizing Error Response Format
The error handler displays errors according to the format setting of the response.
If the the [[yiiwebResponse::format|response format]] is html, it will use the error or exception view
to display errors, as described in the last subsection. For other response formats, the error handler will
assign the array representation of the exception to the [[yiiwebResponse::data]] property which will then
be converted to different formats accordingly. For example, if the response format is json, you may see
the following response:
HTTP/1.1 404 Not Found
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
{
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"code": 0,
"status": 404
}
You may customize the error response format by responding to the beforeSend event of the response component
in the application configuration:
return [
// ...
'components' => [
'response' => [
'class' => 'yiiwebResponse',
'on beforeSend' => function ($event) {
$response = $event->sender;
if ($response->data !== null) {
$response->data = [
'success' => $response->isSuccessful,
'data' => $response->data,
];
$response->statusCode = 200;
}
},
],
],
];
The above code will reformat the error response like the following:
HTTP/1.1 200 OK
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8
{
"success": false,
"data": {
"name": "Not Found Exception",
"message": "The requested resource was not found.",
"code": 0,
"status": 404
}
}
В каждом ответе Http-протокола существует некий 3-х значный код. Он позволяет клиенту правильно трактовать полученный результат. Думаю всем веб-мастерам хорошо известен код «404 — Not found». Кодов на самом деле огромное множество, все они описаны в спецификации Http протокола. Но знать все не обязательно. Главное при создании Exception (исключения) выбрать правильную группу кодов ответа. Групп всего 5:
- 1xx Информационные сообщения
- 2xx Успешное выполнение запроса
- 3xx Перенаправления (редирект)
- 4xx Ошибки клиента
- 5xx Ошибка на сервере
К ошибкам клиента относятся обращения к несуществующей странице, к запрещенной странице, неправильные параметры запроса, вызов не существующего метода, слишком большое количество запросов.
К серверным ошибкам относятся внутренние ошибки сервера, недоступность сервиса или отсутствие поддержки данной версии протокола.
Из всего вышесказанного становится понятно, что разработчику для корректной обработки ошибок нужны коды из 4 группы.
HttpException в Yii2
Зачем вообще нужны Exception? Для того, чтобы в случаи неожиданной ошибки, приложение могло сообщить о том, что его работа прекращена по такой-то причине. Exception можно отлавливать и обрабатывать, например с помощью конструкции try catch.
В Yii2 есть класс yiiwebHttpException с помощью которого можно создать любое исключение.
throw new HttpException(404 ,'User not found');
Так же существуют классы со звучными именами для наиболее часто используемых типов ошибок, благодаря которым вам не нужно запоминать код ответа.
- BadRequestHttpException — 400 ошибка
- UnauthorizedHttpException — 401 ошибка
- ForbiddenHttpException — 403 ошибка
- NotFoundHttpException — 404 ошибка
- MethodNotAllowedHttpException — 405 ошибка
- NotAcceptableHttpException — 406 ошибка
- ConflictHttpException — 409 ошибка
- GoneHttpException — 410 ошибка
- UnsupportedMediaTypeHttpException — 415 ошибка
- TooManyRequestsHttpException — 429 ошибка
- ServerErrorHttpException — 500 ошибка
Все они отнаследованы от yiiwebHttpException и не нуждаются указании кода ответа.
throw new NotFoundHttpException('User not found');
Установить статус ответа можно и с помощью метода setStatusCode() класса yiiwebResponse
Yii::$app->response->setStatusCode(422);
Это удобно тогда, когда есть необходимость передать что-либо в теле ответа.
