HttpException的异常处理
来源:7-2 从一个错误了解Exception的继承关系
江新丰
2017-11-29
如果输入的网址错误,在生产模式下,返回的信息是“服务器错误”。我觉得可以在控制结构中增加HttpException的异常处理,返回“请求的地址不存在”。代码如下:
if ($e instanceof BaseException) {
$this->code = $e->code;
$this->errorCode = $e->errorCode;
$this->msg = $e->msg;
} elseif ($e instanceof HttpException) {
$this->code = 500;
$this->errorCode = 999;
$this->msg = '请求的地址不存在';
} else {
if (config('app_debug')) {
return parent::render($e);
} else {
$this->code = 500;
$this->errorCode = 999;
$this->msg = '服务器错误~!';
$this->recordErrorLog($e);
}
}----------------------------------------------------------------------------------------------------------
更新:
除了输入的网址错误,还有可能是请求的方式错误,两者都属于HttpException异常。如果能区分两者最好,如果区分不了,可以提示“请求的地址不存在或请求的方式错误”。
写回答
4回答
-
7七月
2017-12-02
可以的,这个处理挺好。
20 -
旺仔小牛牛
2019-05-07
受用了
00 -
薛定谔的猫XS
2019-03-03
hhh这个处理很好 我也是遇到了生产模式下的问题
00 -
yuzhewo
2017-11-29
有道理
00
相似问题