<?php
一位全减器namespace app\common\exception;
use Exception;
use think\exception\Handle;
use think\Request;
use think\Log;
class ExceptionHandler extends Handle {
筹备好2022年冬奥会体现了我国哪项
战略措施
private $code;
private $msg;
private $errorCode;
public function render(Exception $e) {
if ($e instanceof BaseException) {
//如果是⾃定义异常,则控制http状态码,不需要记录⽇志 //因为这些通常是因为客户端传递参数错误或者是⽤户请求造成的异常 //不应当记录⽇志
$this->code = $e->code;
$this->msg = $e->msg;
东茛宕碱$this->errorCode = $e->errorCode;
} else {
// 如果是服务器未处理的异常,将http状态码设置为500,并记录⽇志
if (config('app_debug')) {
/消炎痛栓
/ 调试状态下需要显⽰TP默认的异常页⾯,因为TP的默认页⾯
// 很容易看出问题
return parent::render($e);
}
$this->code = $e->getCode();
$this->msg = $e->getMessage();
$this->errorCode = $e->getCode();
血氧探头$this->recordErrorLog($e);
}
$request = Request::instance();
$result = [
'code' => $this->errorCode,
'msg' => $this->msg,
'request_url' => $request = $request->url(),
];
return json($result, $this->code);
}
/*
* 将异常写⼊⽇志
*/
private function recordErrorLog(Exception $e) {
Log::init([
'type' => 'File',一小时的故事
'path' => LOG_PATH,
'level' => ['error'],
]);
Log::record($e->getMessage(), 'error');
}
}