ExceptionHandler.php
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace app\common\exception;
use think\exception\Handle;
use think\Log;
use Exception;
/**
* 重写Handle的render方法,实现自定义异常消息
* Class ExceptionHandler
* @package app\common\library\exception
*/
class ExceptionHandler extends Handle
{
private $code;
private $message;
/**
* 输出异常信息
* @param Exception $e
* @return \think\Response|\think\response\Json
*/
public function render(Exception $e)
{
if ($e instanceof BaseException) {
$this->code = $e->code;
$this->message = $e->message;
} else {
if (config('app_debug')) {
return parent::render($e);
}
$this->code = 0;
$this->message = $e->getMessage() ?: '很抱歉,服务器内部错误';
$this->recordErrorLog($e);
}
return json(['code' => $this->code, 'msg' => $this->message]);
}
/**
* 将异常写入日志
* @param Exception $e
*/
private function recordErrorLog(Exception $e)
{
Log::record($e->getMessage(), 'error');
Log::record($e->getTraceAsString(), 'error');
}
}