yii1tech/error-handler
最新稳定版本:1.0.0
Composer 安装命令:
composer require yii1tech/error-handler
包简介
Provides Enhanced Error Handler for Yii1 application
README 文档
README
Yii1 Enhanced Error Handler
This extension provides Enhanced Error Handler for Yii1 application.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii1tech/error-handler
or add
"yii1tech/error-handler": "*"
to the "require" section of your composer.json.
Usage
This extension provides Enhanced Error Handler for Yii1 application.
Its main feature is conversion of the PHP errors into exceptions, so they may be processed via try..catch blocks.
Note: in order for error to exception conversion to work, the error handler component should be added to the application "preload" section.
Application configuration example:
<?php return [ 'preload' => [ 'errorHandler', // preload custom error handler, overriding the default one, allowing error to exception conversion // ... ], 'components' => [ 'errorHandler' => [ 'class' => \yii1tech\error\handler\ErrorHandler::class, ], // ... ], // ... ];
Once configured \yii1tech\error\handler\ErrorHandler allows you to catch PHP errors as exceptions.
For example:
<?php try { // ... trigger_error('Some custom error message', E_USER_WARNING); // ... } catch (ErrorException $exception) { // handle error }
In addition, \yii1tech\error\handler\ErrorHandler provides support for error/exception rendering as JSON output,
which is useful for modern XHR and API implementation.
By default, the error will be rendered as JSON only in case the HTTP client passes header "Accept" with matching MIME type -
"application/json". However, you may control this behavior using \yii1tech\error\handler\ErrorHandler::$shouldRenderErrorAsJsonCallback.
For example:
<?php return [ 'preload' => [ 'errorHandler', // preload custom error handler, overriding the default one, allowing error to exception conversion // ... ], 'components' => [ 'errorHandler' => [ 'class' => \yii1tech\error\handler\ErrorHandler::class, 'shouldRenderErrorAsJsonCallback' => function () { if (!empty($_SERVER['HTTP_ACCEPT']) && strcasecmp($_SERVER['HTTP_ACCEPT'], 'application/json') === 0) { return true; } if (Yii::app()->request->isAjaxRequest) { return true; } if (str_starts_with(Yii::app()->request->getPathInfo(), 'api/')) { return true; } return false; }, ], // ... ], // ... ];
You may reuse the error handler ability to render errors as JSON in your custom error action, which is specified
via \CErrorHandler::$errorAction. For example:
<?php class SiteController extends CController { public function actionError(): void { /** @var \yii1tech\error\handler\ErrorHandler $errorHandler */ $errorHandler = Yii::app()->getErrorHandler(); if ($errorHandler->shouldRenderErrorAsJson()) { // render JSON error representation. $errorHandler->renderErrorAsJson(); return; } // Render HTML error representation: if ($error = $errorHandler->error) { $this->render('error', $error); } } }
统计信息
- 总下载量: 4.52k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2024-04-24