11 passport 异常该怎么处理呢?

laravel 11 passport OAuthServerException 各位大佬是怎么处理的呀?
我在 bootstrap/app.php 下面进行处理,貌似无效:

->withExceptions(function (Exceptions $exceptions) {
   $exceptions->render(function (\League\OAuth2\Server\Exception\OAuthServerException $e) {
            return response()->json([
                'code'=>config('httpCode.success.status',200),
                'message'=> __($e->getMessage())
            ]);
        });
}

换成\Laravel\Passport\Exceptions\OAuthServerException也无法处理。
有大佬指点一二吗?

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 17

以前是用哪個異常去抓的 現在也用那個去抓就好了或者說你看一下那個異常的類具體是哪個 還有一種情況renderable 是按你寫的順序去執行的 如果在前面就被Throwable抓到了 後面的就不會執行了

你也可以用以前的方式 像這樣就可以了

file

6个月前 评论
carveybunt (楼主) 6个月前
随波逐流
<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {

        $exceptions->render(function (Illuminate\Auth\AuthenticationException $exception) {
            return response()->json([
                'code' => 401,
                'message' => $exception->getMessage(),
            ]);
        });

    })->create();

个人建议就默认使用框架/扩展(passport)异常,原样输出就可以了,符合 http 规范。

6个月前 评论
carveybunt (楼主) 6个月前
随波逐流 (作者) 6个月前
carveybunt (楼主) 6个月前
随波逐流 (作者) 6个月前
carveybunt (楼主) 6个月前

file
你是说的这种吗,代码如下

<?php

use App\Enums\Api\Response as CustomResponse;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        $exceptions->render(function (Throwable $e) {
            if ($e instanceof ModelNotFoundException) {
                return  apiResponse(CustomResponse::SYSTEM_ERROR->value,[],CustomResponse::SYSTEM_ERROR->response());
            }
            //非允许请求方式
            if ($e instanceof MethodNotAllowedHttpException) {
                return  apiResponse(CustomResponse::SYSTEM_REQUEST_ERROR->value,[],CustomResponse::SYSTEM_REQUEST_ERROR->response());
            }
            //验证失败
            if ($e instanceof ValidationException) {
                return  apiResponse(CustomResponse::SYSTEM_CHECK_ERROR->value,[],array_values($e->validator->getMessageBag()->toArray())[0][0]);
            }
            //验证token
            if ($e instanceof AuthenticationException || $e instanceof AuthorizationException) {
                return  apiResponse(CustomResponse::TOKEN_ERROR->value,[],CustomResponse::TOKEN_ERROR->response());
            }
            //这里是为了兼容其他的一些错误
            if (Str::length($e->getMessage()) > 1 && Str::length($e->getCode()) > 1) {
                return apiResponse($e->getCode(),[], $e->getMessage());
            }
            if ($e instanceof NotFoundHttpException) {
                return  apiResponse(CustomResponse::SYSTEM_NOT_FIND->value,[], CustomResponse::SYSTEM_NOT_FIND->response());
            }
            return  apiResponse(CustomResponse::SERVICE_OTHER_ERROR->value,[], CustomResponse::SERVICE_OTHER_ERROR->response());
        });
    })->withProviders()->create();
6个月前 评论
Shine-x (作者) 6个月前
carveybunt (楼主) 6个月前
Shine-x (作者) 6个月前
carveybunt (楼主) 6个月前
Shine-x (作者) 6个月前
carveybunt (楼主) 6个月前
随波逐流
<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {

        // 取消日志上报
        $exceptions->dontReport([
            'Laravel\Passport\Exceptions\OAuthServerException'
        ]);

    })->create();
6个月前 评论
carveybunt (楼主) 6个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!