<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
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;


/**
 * 异常处理器
 * 统一捕获和格式化框架异常
 * @package app
 */
class ExceptionHandle extends Handle
{
    /**
     * 忽略报告的异常类
     * @var array
     */
    protected $ignoreReport = [
        HttpException::class,
        HttpResponseException::class,
        ModelNotFoundException::class,
        DataNotFoundException::class,
        ValidateException::class,
    ];

    /**
     * 报告异常
     * @param Throwable $exception
     * @return void
     */
    public function report(Throwable $exception): void
    {

        parent::report($exception);
    }

    /**
     * 渲染异常
     * @param \think\Request   $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {
        if ($e instanceof ValidateException) {
            return json([
                'code' => 422,
                'msg'  => $e->getMessage(),
                'data' => null,
            ])->code(422);
        }

        return parent::render($request, $e);
    }
}