由于管理后台几乎所有的请求都是通过 ajax 请求的,默认的异常直接抛出,会导致 ajax 返回的 json 个数错误,于是对框架抛出的异常做了自定义处理,将 ajax 请求时的异常以 json 格式抛出,其他异常继承与异常处理方法
```
use thinkexceptionHandle;
use thinkexceptionHttpException;
use thinkRequest;
class TpException extends Handle
{
public function render(Exception $e)
{
if ($e instanceof HttpException) {
$statusCode = $e->getStatusCode();
http_response_code($statusCode);
}
//可以在此交由系统处理
if (Request::instance()->isAjax()) {
$error_code = $this->getCode($e) ?: 1;
return ajax_return_adv_error($this->getMessage($e), $error_code);
}
return parent::render($e);
}
}
```
如果需要请修改 `extendTpException.php` 文件
>[info] 以后在使用时无论是否有 ajax 请求,都可以轻松抛出一个异常,都不会导致没有正确的 json 格式数据返回