CRMEB/crmeb/app/kefuapi/KefuApiExceptionHandle.php
2021-07-13 16:49:21 +08:00

63 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\kefuapi;
use crmeb\exceptions\AuthException;
use think\exception\Handle;
use think\exception\ValidateException;
use think\facade\Config;
use think\Response;
use Throwable;
class KefuApiExceptionHandle extends Handle
{
/**
* 记录异常信息(包括日志或者其它方式记录)
*
* @access public
* @param Throwable $exception
* @return void
*/
public function report(Throwable $exception): void
{
// 使用内置的方式记录异常日志
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @access public
* @param \think\Request $request
* @param Throwable $e
* @return Response
*/
public function render($request, Throwable $e): Response
{
$massageData = Config::get('app_debug', false) ? [
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTrace(),
'previous' => $e->getPrevious(),
] : [];
// 添加自定义异常处理机制
if ($e instanceof DbException) {
return app('json')->fail('数据获取失败', $massageData);
} elseif ($e instanceof ValidateException || $e instanceof AuthException) {
return app('json')->make($e->getCode() ?: 400, $e->getMessage());
} else {
return app('json')->code(200)->make(400, $e->getMessage(), $massageData);
}
}
}