<?php

namespace app;

use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Response;
use Throwable;
use app\common\arw\adjfut\src\Exception\ErrorMsg;
use app\common\exception\LoginTimeOut;
use app\common\exception\NotAuthApi;
use think\facade\Env;
use app\common\arw\adjfut\src\Exception\ErrorMsg as ExceptionErrorMsg;

/**
 * 应用异常处理类
 */
class ExceptionHandle extends Handle
{
    /**
     * 不需要记录信息（日志）的异常类列表
     * @var array
     */
    protected $ignoreReport = [
        HttpException::class,
        HttpResponseException::class,
        ModelNotFoundException::class,
        DataNotFoundException::class,
        ValidateException::class,
        LoginTimeOut::class,
        NotAuthApi::class,
        ErrorMsg::class,
        ExceptionErrorMsg::class
    ];

    /**
     * 记录异常信息（包括日志或者其它方式记录）
     *
     * @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 \app\Request   $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {
        // 添加自定义异常处理机制
        // 未授权接口
        if ($e instanceof NotAuthApi) {
            return $this->respone([
                'code' => 40050,
                'msg' => $e->getMessage(),
            ]);
        }
        // 登录超时
        if ($e instanceof LoginTimeOut) {
            return $this->respone([
                'code' => 40026,
                'msg' => $request->isDev() ? $e->getMessage() : '登录超时',
            ]);
        }
        // 参数验证不通过
        if ($e instanceof ValidateException) {
            return $this->respone([
                'code' => 40030,
                'msg' => $e->getMessage()
            ]);
        }
        // 业务异常
        if ($e instanceof ErrorMsg || $e instanceof ExceptionErrorMsg) {
            $code = $e->getCode();
            return $this->respone([
                'code' => $code == 0 ? 40052 : $code,
                'msg' => $e->getMessage(),
            ], $e);
        }
        // 服务器异常
        //        if ($e instanceof Exception) {
        //            return $this->respone([
        //                'code' => 40051,
        //                'msg' => '服务器异常',
        //            ], $e);
        //        }
        // 其他错误交给系统处理
        return parent::render($request, $e);
    }

    /**
     * 返回
     *
     * @param array $data
     * @param Throwable $e
     * @return Response
     * @date 2022-04-15
     * @example
     * @author admin
     * @since 1.0.0
     */
    private function respone(array $data, $e = null): Response
    {
        $request = $this->app->request;
        if ($e && Env::get('app_show_error')) {
            $data['__error'] = [
                'File' => $e->getFile(),
                'Line' => $e->getLine(),
                'Message' => $e->getMessage(),
                'Trace' => $e->getTrace()
            ];
            $data['__param'] = $request->param();
        }
        if ($request->isAjax()) {
            return Response::create($data, 'json');
        } else {
            return Response::create(json_encode($data), 'html');
        }
    }
}
