Laravel的Guzzle错误。 RequestException.php第107行中的ClientException:

时间:2016-09-13 19:14:54

标签: laravel guzzle

这是我的构造函数

public function __construct()
{
    $this->jar = new \GuzzleHttp\Cookie\CookieJar();
    $this->guzzle_instance = new \GuzzleHttp\Client([
        'base_uri' => API_URI,
        'cookies' => $this->jar
    ]);
    return $this;
}

以下是尝试登录API的函数

 protected function processRequest($method, $sub_uri, $body = null, $headers = null)
{
    $this->response = $this->guzzle_instance->request($method, $sub_uri, array(
        'headers' => $headers,
        'body' => $body
    ));
}

最后,这是检查,会在成功登录时重定向我或向用户发送某种消息

  if ($authzor->attemptLogin($request['username'], $request['password'])) {
        return redirect()->intended('dashboard');
    } else {
        /**
         * this is where i get the exception for login's that don't pass
         */

        return "Username or password is missing." . $authzor->getResponse();
    }

如果我传递了正确的凭据,它会将我重定向到仪表板,否则我在RequestException.php第107行中收到错误ClientException:

最后但并非最不重要的,这是跟踪堆栈

ClientException in RequestException.php line 107:
Client error: `GET http://<host address>/api/v1/login/admin/testerson` 

resulted in a `403 Forbidden` response:
{"timestamp":"2016-09-13T13:35:23-05:00","api_version":"v1","success":"false","message":"Invalid username\/password comb (truncated...)
in RequestException.php line 107
at RequestException::create(object(Request), object(Response)) in Middleware.php line 65
at Middleware::GuzzleHttp\{closure}(object(Response)) in Promise.php line 203
at Promise::callHandler('1', object(Response), array(object(Promise), object(Closure), null)) in Promise.php line 156
at Promise::GuzzleHttp\Promise\{closure}() in TaskQueue.php line 61
at TaskQueue->run(true) in Promise.php line 246
at Promise->invokeWaitFn() in Promise.php line 223
at Promise->waitIfPending() in Promise.php line 266
at Promise->invokeWaitList() in Promise.php line 225
at Promise->waitIfPending() in Promise.php line 62
at Promise->wait() in Client.php line 129
at Client->request('GET', 'login/admin/jdiscool', array('headers' => array('Content-Type' => 'application/json'), 'body' => null)) in ApiClient.php line 60
at ApiClient->processRequest('GET', 'login/admin/jdiscool') in MOAuthenticator.php line 21
at MOAuthenticator->attemptLogin('admin', 'jdiscool') in AuthController.php line 58
at AuthController->authenticate(object(Request))
at call_user_func_array(array(object(AuthController), 'authenticate'), array(object(Request))) in Controller.php line 80
at Controller->callAction('authenticate', array(object(Request))) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(AuthController), object(Route), 'authenticate') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in RedirectIfAuthenticated.php line 24
at RedirectIfAuthenticated->handle(object(Request), object(Closure))
at call_user_func_array(array(object(RedirectIfAuthenticated), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(AuthController), object(Route), object(Request), 'authenticate') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\Auth\AuthController', 'authenticate') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 64
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 51

1 个答案:

答案 0 :(得分:4)

我修改了你的功能并将你的请求放入try / catch中,这样你就可以处理200以上的其他响应....

protected function processRequest($method, $sub_uri, $body = null, $headers = null)
{
    try {
         $this->response = $this->guzzle_instance->request($method, $sub_uri, array(
        'headers' => $headers,
        'body' => $body
        ));    
   }
   catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
        $result =  $response->getBody();

        //do something with it....

   }
}