1 Star 0 Fork 26

arthur_liang/yii2-fast-api

forked from deepziyu/yii2-fast-api 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ErrorHandler.php 5.00 KB
一键复制 编辑 原始数据 按行查看 历史
deepziyu 提交于 2017-01-20 16:38 . fix 完善注释信息
<?php
namespace deepziyu\yii\rest;
use Yii;
use yii\base\Exception;
use yii\base\ErrorException;
use yii\base\UserException;
use yii\web\HttpException;
use yii\web\Response;
use yii\helpers\VarDumper;
/**
* Class ErrorHandler
* @package deepziyu\yii\rest
*/
class ErrorHandler extends \yii\web\ErrorHandler
{
/**
* @var integer maximum number of source code lines to be displayed. Defaults to 19.
*/
public $maxSourceLines = 10;
/**
* @var integer maximum number of trace source code lines to be displayed. Defaults to 13.
*/
public $maxTraceSourceLines = 5;
/**
* Renders the exception.
* @param \Exception $exception the exception to be rendered.
*/
protected function renderException($exception)
{
if (Yii::$app->has('response')) {
$response = Yii::$app->getResponse();
// reset parameters of response to avoid interference with partially created response data
// in case the error occurred while sending the response.
$response->isSent = false;
$response->stream = null;
$response->data = null;
$response->content = null;
} else {
$response = new Response();
}
$useErrorView = $response->format === Response::FORMAT_HTML && (!YII_DEBUG || $exception instanceof UserException);
if ($useErrorView && $this->errorAction !== null) {
$result = Yii::$app->runAction($this->errorAction);
if ($result instanceof Response) {
$response = $result;
} else {
$response->data = $result;
}
} elseif ($response->format === Response::FORMAT_HTML) {
if (YII_ENV_TEST || isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') {
// AJAX request
$response->data = '<pre>' . $this->htmlEncode(static::convertExceptionToString($exception)) . '</pre>';
} else {
// if there is an error during error rendering it's useful to
// display PHP error in debug mode instead of a blank screen
if (YII_DEBUG) {
ini_set('display_errors', 1);
}
$file = $useErrorView ? $this->errorView : $this->exceptionView;
$response->data = $this->renderFile($file, [
'exception' => $exception,
]);
}
} elseif ($response->format === Response::FORMAT_RAW) {
$response->data = static::convertExceptionToString($exception);
} else {
$response->data = $this->convertExceptionToArray($exception);
}
$response->setStatusCode(200);
$response->send();
}
/**
* Converts an exception into an array.
* @param \Exception $exception the exception being converted
* @return array the array representation of the exception.
*/
protected function convertExceptionToArray($exception)
{
if (!YII_DEBUG && !$exception instanceof UserException && !$exception instanceof HttpException) {
$exception = new HttpException(500, Yii::t('yii', 'An internal server error occurred.'));
}
$array = [
'code' => $exception->getCode(),
'data' => new \stdClass(),
'message' => $exception->getMessage(),
];
if ($exception instanceof HttpException) {
$array['code'] = $exception->statusCode;
}
if($exception instanceof ApiException && !empty($exception->model)){
$array = [
'code' => 422,
'data' => $exception->model->getErrors(),
'message' => 'Data Validation Failed.',
];
}
if (YII_DEBUG) {
$array['type'] = get_class($exception);
if (!$exception instanceof UserException) {
$array['file'] = $exception->getFile();
$array['line'] = $exception->getLine();
$array['stack-trace'] = explode("\n", $exception->getTraceAsString());
if ($exception instanceof \yii\db\Exception) {
$array['error-info'] = $exception->errorInfo;
}
}
if (($prev = $exception->getPrevious()) !== null) {
$array['previous'] = $this->convertExceptionToArray($prev);
}
}
return $array;
}
// /**
// * Returns human-readable exception name
// * @param \Exception $exception
// * @return string human-readable exception name or null if it cannot be determined
// */
// public function getExceptionName($exception)
// {
// if ($exception instanceof \yii\base\Exception || $exception instanceof \yii\base\InvalidCallException || $exception instanceof \yii\base\InvalidParamException || $exception instanceof \yii\base\UnknownMethodException) {
// return $exception->getName();
// }
// return null;
// }
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/arthur_liang/yii2-fast-api.git
git@gitee.com:arthur_liang/yii2-fast-api.git
arthur_liang
yii2-fast-api
yii2-fast-api
master

搜索帮助