How to add status in default response passport

会有一股神秘感。 提交于 2019-12-01 09:38:53

You could create a new exception handler middleware to catch these requests and modify the response it returns.

Example:

class oAuthExceptionHandler {
    public function handle($request, Closure $next) {
        try {
            $response = $next($request);

            if (isset($response->exception) && $response->exception) {
                throw $response->exception;
            }

            return $response;
        } catch (\Exception $e) {
            return response()->json(array(
                'result' => 0,
                'msg' => $e->getMessage(),
            ), 401);
        }
    }
}

Then, in your app/Http/Kernel.php, name your middleware and add it into your api group:

protected $routeMiddleware = [
    'oauth_exception' => oAuthExceptionHandler::class,
    ...
];

protected $middlewareGroups = [
    ...

    'api' => [
        'oauth_exception',
        ...
    ],
];

You can handle the api exception and format its response in app/Exceptions/Handler.php.

Here is the link that you can follow

Laravel API, how to properly handle errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!