laravel护照如何优雅地处理错误

时间:2019-02-26 09:21:24

标签: laravel-passport

我正在尝试使用laravel5.7 + Passport来做我的API服务器(dingo软件包与我的某些软件包不兼容,我必须放弃它)。我希望全局对象与API错误处理更加兼容,因此我修改了App\Exceptions\Handler

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{

    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        // Determine whether it is an API interface
        if($request->is('api/*')) {
            $response = [];
            $error = $this->convertExceptionToResponse($exception);
            $response['message'] = $exception->getMessage();
            $response['status_code'] = $error->getStatusCode();
            if(config('app.debug')) {
                if($error->getStatusCode() >= 500) {
                    $response['debug']['line'] = $exception->getLine(); // error line
                    $response['debug']['file'] = $exception->getFile(); // error file
                    $response['debug']['class'] = get_class($exception); // error position
                    $response['debug']['trace'] = explode("\n", $exception->getTraceAsString()); //Error stack
                }
            }
            // response api
            return response()->json($response, $error->getStatusCode());
        } else {
            // response web
            return parent::render($request, $exception);
        }
    }
}

错误详细信息:

{
    "message": "Unauthenticated.",
    "status_code": 500,
    "debug": {
        "line": 67,
        "file": "/Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php",
        "class": "Illuminate\\Auth\\AuthenticationException",
        "trace": [
            "#0 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(41): Illuminate\\Auth\\Middleware\\Authenticate->authenticate(Object(Illuminate\\Http\\Request), Array)",
            "#1 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Auth\\Middleware\\Authenticate->handle(Object(Illuminate\\Http\\Request), Object(Closure), 'api')",
            "#2 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
            "#3 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(58): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
            "#4 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Routing\\Middleware\\ThrottleRequests->handle(Object(Illuminate\\Http\\Request), Object(Closure), 60, '1')",
            "#5 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
            "#6 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
            "#7 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(684): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
            "#8 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(659): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))",
            "#9 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(625): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))",
            "#10 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(614): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))",
            "#11 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))",
            "#12 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))",
            "#13 /Users/noecs/Desktop/noecsSystemLaravel/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
            "#14 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
            "#15 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
            "#16 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(31): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
            "#17 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
            "#18 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
            "#19 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(31): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
            "#20 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
            "#21 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
            "#22 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
            "#23 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
            "#24 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
            "#25 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
            "#26 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(163): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
            "#27 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
            "#28 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
            "#29 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
            "#30 /Users/noecs/Desktop/noecsSystemLaravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))",
            "#31 /Users/noecs/Desktop/noecsSystemLaravel/vendor/swooletw/laravel-swoole/src/Server/Sandbox.php(257): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))",
            "#32 /Users/noecs/Desktop/noecsSystemLaravel/vendor/swooletw/laravel-swoole/src/Server/Sandbox.php(196): SwooleTW\\Http\\Server\\Sandbox->handleRequest(Object(Illuminate\\Http\\Request))",
            "#33 /Users/noecs/Desktop/noecsSystemLaravel/vendor/swooletw/laravel-swoole/src/Server/Sandbox.php(182): SwooleTW\\Http\\Server\\Sandbox->prepareResponse(Object(Illuminate\\Http\\Request))",
            "#34 /Users/noecs/Desktop/noecsSystemLaravel/vendor/swooletw/laravel-swoole/src/Server/Manager.php(221): SwooleTW\\Http\\Server\\Sandbox->run(Object(Illuminate\\Http\\Request))",
            "#35 {main}"
        ]
    }
}

我尝试输入错误的token,根据API开发规范,我应该返回401错误的令牌。但是出乎我的意料,程序输出是一个500错误。这让我很尴尬,我不知道如何优雅地处理此API的错误消息。

0 个答案:

没有答案