Files
2024-09-27 01:32:49 +08:00

55 lines
1.3 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/5/23
* Time: 15:53
*/
namespace app\lib\exception;
use Exception;
use think\exception\Handle;
use think\facade\Log;
use think\facade\Request;
class ExceptionHandle extends Handle
{
private $code;
private $msg;
private $errorCode;
private $status;
//需要返回客户端当前请求的url路径
public function render(Exception $e){
if($e instanceof BaseException){
//如果是自定义的异常
$this->code = $e->code;
$this->msg = $e->msg;
$this->errorCode = $e->errorCode;
$this->status = $e->status;
}else{
if(config('app_debug')){
return parent::render($e);
}else{
$this->code = 500;
$this->msg = '服务器内部错误';
$this->errorCode = 999;
$this->recordErrorLog($e);
}
}
$result = [
'code' => $this->code,
'msg' => $this->msg,
'error_code' => $this->errorCode,
'status'=>$this->status,
'request_url' =>Request::url()
];
return json($result,$this->code);
}
private function recordErrorLog(Exception $e){
Log::record($e->getMessage(),'error');
}
}