Files
swiftadmin/app/common/exception/ExceptionHandle.php

70 lines
2.3 KiB
PHP
Raw Normal View History

2022-08-19 19:48:37 +08:00
<?php
namespace app\common\exception;
use app\common\model\system\SystemLog;
use Psr\SimpleCache\InvalidArgumentException;
use support\exception\BusinessException;
2022-11-28 19:11:12 +08:00
use think\db\exception\DataNotFoundException;
2023-06-19 14:32:30 +08:00
use think\exception\ValidateException;
2022-11-28 19:11:12 +08:00
use Webman\Exception\ExceptionHandler;
2022-08-19 19:48:37 +08:00
use Webman\Http\Request;
use Webman\Http\Response;
use Throwable;
2022-11-28 19:11:12 +08:00
class ExceptionHandle extends ExceptionHandler
2022-08-19 19:48:37 +08:00
{
public $dontReport = [
BusinessException::class,
2023-06-19 14:32:30 +08:00
ValidateException::class,
DataNotFoundException::class,
OperateException::class,
DumpException::class,
2022-08-19 19:48:37 +08:00
];
2022-11-28 19:11:12 +08:00
/**
2023-04-25 20:11:49 +08:00
* 异常日志记录
2022-11-28 19:11:12 +08:00
* @param Throwable $exception
2023-06-19 14:32:30 +08:00
* @throws InvalidArgumentException
2022-11-28 19:11:12 +08:00
*/
2022-08-19 19:48:37 +08:00
public function report(Throwable $exception)
{
2023-06-19 14:32:30 +08:00
if (saenv('system_exception')
&& !$this->shouldntReport($exception)) {
$logs['module'] = request()->app;
$logs['controller'] = request()->controller;
$logs['action'] = request()->action;
$logs['params'] = serialize(request()->all());
$logs['method'] = request()->method();
$logs['url'] = request()->url();
$logs['ip'] = request()->getRealIp();
$logs['name'] = session('AdminLogin.name') ?? 'system';
$logs['type'] = 1;
$logs['code'] = $exception->getCode();
$logs['file'] = $exception->getFile();
$logs['line'] = $exception->getLine();
$logs['error'] = $exception->getMessage();
SystemLog::write($logs);
2022-08-19 19:48:37 +08:00
}
}
2022-11-28 19:11:12 +08:00
/**
* @param Throwable $exception
* @param Request $request
* @return Response
*/
2022-08-19 19:48:37 +08:00
public function render(Request $request, Throwable $exception): Response
{
2023-06-19 14:32:30 +08:00
switch (true) {
case $exception instanceof OperateException:
case $exception instanceof ValidateException:
return json(['code' => $exception->getCode() ?? 101, 'msg' => $exception->getMessage()]);
case $exception instanceof DumpException:
return \response($exception->getMessage());
default:
break;
}
2023-06-19 14:32:30 +08:00
2023-04-25 20:11:49 +08:00
return get_env('APP_DEBUG') ? parent::render($request, $exception) : view(config('app.exception_tpl'), ['trace' => $exception]);
2022-08-19 19:48:37 +08:00
}
}