Laravel 使用 passport 怎么自定义错误返回

我用 laravel 的 passport 去做接口认证功能。

但是 token 失效的时候,接口报了个错。

local.ERROR: The resource owner or authorization server denied the request.

laravel 使用 passport 怎么自定义错误返回

但是我想返回我自定义的错误信息,而不是这个。

有大佬知道怎么搞吗?

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2

App\Exceptions\Handler 中的 render 方法中判断 $exception 是不是 OAuthServerException 的实例,是的话返回自定义的 JSON 数据,例如这样:

public function render($request, Throwable $exception)
{
    if ($exception instanceof ValidationException) {
        return failed(Arr::first(Arr::collapse($exception->errors())), 422);
    }

    if ($exception instanceof ModelNotFoundException) {
        return notFound();
    }

    if ($exception instanceof NotFoundHttpException) {
        return notFound();
    }

    if ($exception instanceof ThrottleRequestsException) {
        return failed("请求过于频繁", 400);
    }

    return parent::render($request, $exception);
}
4年前 评论
半人间 4年前
zero风来 4年前