12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace app\api\library;
- use app\service\company_wechat\Webhook;
- use app\UserException;
- use Exception;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\Handle;
- /**
- * 自定义API模块的错误显示
- */
- class ExceptionHandle extends Handle
- {
- protected $ignoreReport = [
- '\\think\\exception\\HttpException',
- UserException::class,
- ];
- public function render(Exception $e)
- {
- $statuscode = $code = 0;
- $msg = '服务器错误';
- // 验证异常
- if ($e instanceof \think\exception\ValidateException) {
- $code = 0;
- $statuscode = 200;
- $msg = $e->getError();
- }
- // Http异常
- elseif ($e instanceof \think\exception\HttpException) {
- $statuscode = $code = $e->getStatusCode();
- }
- elseif ($e instanceof UserException) {
- $code = 0;
- $msg = $e->getMessage();
- $statuscode = 200;
- }
- elseif ($e instanceof ModelNotFoundException) {
- $msg = '查找的数据不存在';
- }else{
- //Webhook::push($e);
- }
- if(config('app_debug')){
- $msg=$e->getMessage();
- }
- // 在生产环境下返回code信息
- //if (!\think\Config::get('app_debug')) {
- if(request()->isAjax() || strpos(request()->header('Accept'),'/json')!==false) {
- //$msg = 'An error occurred';
- return json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null], $statuscode);
- }
- //}
- //其它此交由系统处理
- return parent::render($e);
- }
- }
|