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

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

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

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

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

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

有大佬知道怎么搞吗?

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《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年前

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