webman 多应用异常处理,如何和默认的异常处理共存呢

tzbob888

问题描述

自定义后台master应用的异常是可以生效的,但是自定义以后原先的
'' => support\exception\Handler::class,失效了
我需要在访问后台的时候 master应用的异常是ok的
但是也需要访问原先 app/controller里面的控制器异常也ok该如何处理呢?

<?php
namespace app\exception;

use Throwable;
use PDOException;
use Illuminate\Database\QueryException;
use Webman\Exception\ExceptionHandler;
use Webman\Http\Request;
use Webman\Http\Response;

class MasterHandler extends ExceptionHandler
{
    /**
     * Jacob
     * 2025-02-10 17:00:00
     * 不需要被记录的异常
     * @var array
     */
    public $dontReport = [
        //QueryException::class,
        // Add other exceptions as needed
    ];

    /**
     * Jacob
     * 2025-02-10 17:00:00
     * 写入到日志文件中
     *  @param Throwable $exception
     */
    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    /**
     * Jacob
     * 2025-02-10 17:00:00
     * 将相应的异常渲染到 HTTP 响应中
     *  @param Request $request
     *  @param Throwable $exception
     *  @return Response
     */
    public function render(Request $request, Throwable $exception): Response
    {
        if ($exception instanceof PDOException || $exception instanceof QueryException) {
            return json([
                'code' => $exception->getCode(),
                'msg' => $exception->getMessage(),
                //'trace' => $exception->getTraceAsString()
            ]);
        }
        // Handle other exceptions
        return parent::render($request, $exception);
    }
}

/config/exception.php 异常配置文件

<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

return [
    '' => support\exception\Handler::class,
    'master' => app\exception\MasterHandler::class,
];

目录结构
截图

为此你搜索到了哪些方案及不适用的原因

我现在master应用里面的方法异常是ok的,但是/app/controller/indexController.php这个目录的异常处理却无效了,有没有什么办法,让默认的目录/app/controller/ 里面的控制器还是使用以前的异常处理或者我单独给他定义一个是如何定义呢?

感谢各位大佬!

111 1 0
1个回答

tzbob888

问题已解决直接把全局的自定义一份即可,之所以自定义是想接口返回异常的时候格式化成json数据以方便前端处理
异常配置文件

<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

return [
    '' => app\exception\Handler::class,//默认全局异常处理
    'master' => app\exception\MasterHandler::class,//后台异常处理
];

新的自定义全局异常处理

<?php
namespace app\exception;

use Throwable;
use PDOException;
use Illuminate\Database\QueryException;
use Webman\Exception\ExceptionHandler;
use support\exception\PageNotFoundException;
use support\exception\MethodNotAllowedException;
use support\exception\BusinessException;
use support\exception\UnauthorizedException;
use support\exception\InvalidRequestException;
use support\exception\InvalidResponseException;
use support\exception\InvalidParameterException;
use support\exception\InvalidModelException;
use support\exception\InvalidTokenException;
use support\exception\InvalidSignatureException;
use support\exception\InvalidCredentialException;
use support\exception\InvalidPermissionException;
use support\exception\InvalidRoleException;
use support\exception\InvalidResourceException;
use support\exception\InvalidStateException;
use Webman\Http\Request;
use Webman\Http\Response;

class Handler extends ExceptionHandler
{
    /**
     * Jacob
     * 2025-02-10 17:00:00
     * 不需要被记录的异常
     * @var array
     */
    public $dontReport = [
        //QueryException::class,
        // Add other exceptions as needed
    ];

    /**
     * Jacob
     * 2025-02-10 17:00:00
     * 写入到日志文件中
     *  @param Throwable $exception
     */
    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    /**
     * Jacob
     * 2025-02-10 17:00:00
     * 将相应的异常渲染到 HTTP 响应中
     *  @param Request $request
     *  @param Throwable $exception
     *  @return Response
     */
    public function render(Request $request, Throwable $exception): Response
    {
        if ($exception instanceof PDOException || $exception instanceof QueryException || $exception instanceof PageNotFoundException) {
            return json([
                'code' => $exception->getCode(),
                'msg' => $exception->getMessage(),
                //'trace' => $exception->getTraceAsString()
            ]);
        }
        // Handle other exceptions
        return parent::render($request, $exception);
    }
}
  • 暂无评论
×
🔝