<?php

namespace app;

use Throwable;
use think\Response;
use think\facade\Log;
use app\common\util\Result;
use think\exception\Handle;
use Firebase\JWT\ExpiredException;
use think\exception\HttpException;
use Firebase\JWT\BeforeValidException;
use think\exception\ValidateException;
use app\common\exception\CatchException;
use think\exception\HttpResponseException;
use Firebase\JWT\SignatureInvalidException;
use app\common\exception\ParentHttpException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

/**
 * 应用异常处理类
 */
class ExceptionHandle extends Handle
{
    /**
     * 不需要记录信息(日志)的异常类列表
     * @var array
     */
    protected $ignoreReport = [
        HttpException::class,
        HttpResponseException::class,
        ModelNotFoundException::class,
        DataNotFoundException::class,
        //ValidateException::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 \think\Request $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {
        $className = get_class($e);
        if (!in_array($className, $this->ignoreReport)) {
            Log::debug("尝试捕获异常[{$e->getCode()}]:" . get_class($e) . " | 提示消息:{$e->getMessage()}" . ($e->getPrevious() ? ' | 父错误:' . get_class($e->getPrevious()) : ''));
        }

        // 应该被捕获的异常
        if ($e instanceof CatchException) {
            // 常规错误
            return Result::restf($e->getCode(), $e->getMessage());
        }

        // 参数验证错误
        if ($e instanceof ValidateException) {
            return Result::restf(777, "参数验证错误:\n " . $e->getMessage());
        }

        if ($e instanceof SignatureInvalidException) {
            // provided JWT signature verification failed.
            return Result::restf(601, '无效的Jwt签名');
        } elseif ($e instanceof BeforeValidException) {
            // provided JWT is trying to be used before "nbf" claim OR
            // provided JWT is trying to be used before "iat" claim.
            return Result::restf(602, 'Jwt在授权期之前');
        } elseif ($e instanceof ExpiredException) {
            // provided JWT is trying to be used after "exp" claim.
            return Result::restf(603, 'Jwt授权已过期');
        }

        // 请求异常
        if ($e instanceof HttpException && request()->isAjax()) {
            return response($e->getMessage(), $e->getStatusCode());
        }
        if (!in_array($className, $this->ignoreReport)) {
            Log::error('未捕获异常:' . $e->getTraceAsString());
        }

        // 其他错误交给系统处理
        return parent::render($request, $e);
    }
}