thinkphp5接口自定义错误处理

thinkphp5接⼝⾃定义错误处理
<?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');
}
}

本文发布于:2024-09-23 14:32:44,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/153206.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:需要   状态   战略   错误   造成
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议