Get the associated response
public getResponse ( ) : Psr\Http\Message\ResponseInterface | null | ||
return | Psr\Http\Message\ResponseInterface | null |
/** * @param RequestException $ex * @return AccessDeniedException|EntityNotFoundException|EntityValidationException|RequiredFieldMissingException|UnauthorizedClientException|CaravanaHttpException */ public static function parseRequestException(RequestException $ex) { $code = $ex->getCode(); $body = json_decode($ex->getResponse()->getBody()->getContents(), true); if (CaravanaExceptionFactory::isEntityValidationException($code, $body)) { return new EntityValidationException(AU::get($body['Entity']), AU::get($body['field']), AU::get($body['reason']), AU::get($body['providedValue']), $ex->getPrevious()); } else { if (CaravanaExceptionFactory::isRequiredFieldMissingException($code, $body)) { return new RequiredFieldMissingException(AU::get($body['Entity']), AU::get($body['field']), $ex->getPrevious()); } else { if (CaravanaExceptionFactory::isUnauthorizedClientException($code, $body)) { return new UnauthorizedClientException($ex->getMessage(), $ex->getPrevious()); } else { if (CaravanaExceptionFactory::isAccessDeniedException($code, $body)) { return new AccessDeniedException($ex->getMessage(), $ex->getPrevious()); } else { if (CaravanaExceptionFactory::isEntityNotFoundException($code, $body)) { return new EntityNotFoundException(AU::get($body['Entity']), AU::get($body['field']), AU::get($body['providedValue']), $ex->getPrevious()); } } } } } // Give up and default it to CaravanaException return new CaravanaHttpException($ex->getMessage(), $ex->getCode(), null, $ex->getPrevious()); }
/** * Converts a Guzzle exception into an Httplug exception. * * @param RequestException $exception * * @return Exception */ private function createException(RequestException $exception) { if ($exception->hasResponse()) { return new HttpException($exception->getMessage(), $exception->getRequest(), $exception->getResponse(), $exception); } return new NetworkException($exception->getMessage(), $exception->getRequest(), $exception); }
private function handleException(RequestException $exception) { if ($exception instanceof BadResponseException) { return $exception->getResponse(); } throw new Exception($exception->getMessage()); }
/** * @internal * @param RequestException $e * @return AccessDeniedException|ApiException|AuthenticationException|ConflictingStateException| * MethodNotAllowedException|NotFoundException|RateLimitExceededException|UnsupportedAcceptHeaderException| * UnsupportedContentTypeException|ValidationException */ public static function create(RequestException $e) { if ($response = $e->getResponse()) { switch ($response->getStatusCode()) { case 400: return new ValidationException($e); case 401: return new AuthenticationException($e); case 403: return new AccessDeniedException($e); case 404: return new NotFoundException($e); case 405: return new MethodNotAllowedException($e); case 406: return new UnsupportedAcceptHeaderException($e); case 409: return new ConflictingStateException($e); case 415: return new UnsupportedContentTypeException($e); case 429: return new RateLimitExceededException($e); } } return new ApiException($e); }
private function manejar_excepcion_request(RequestException $e) { $msg = $e->getRequest() . "\n"; if ($e->hasResponse()) { $msg .= $e->getResponse() . "\n"; } throw new toba_error($msg); }
/** * throwRequestException * * @param \GuzzleHttp\Exception\RequestException $e * @param string $className * @throws Exceptions\RequestException */ protected function throwRequestException($e, $className) { $response = $e->getResponse(); $rawBody = (string) $response->getBody(); $body = json_decode((string) $rawBody); $body = $body === null ? $rawBody : $body; $exception = new $className($e->getMessage(), $e->getCode()); $exception->setBody($body); throw $exception; }
protected function handleError(RequestException $e) { $response = $e->getResponse(); switch ($response->getStatusCode()) { case 404: throw new Exceptions\NotFoundException($this->getResponseError($response)); default: throw new Exceptions\MeshException($this->getResponseError($response)); } }
public function testHasRequestAndResponse() { $req = new Request('GET', '/'); $res = new Response(200); $e = new RequestException('foo', $req, $res); $this->assertSame($req, $e->getRequest()); $this->assertSame($res, $e->getResponse()); $this->assertTrue($e->hasResponse()); $this->assertEquals('foo', $e->getMessage()); }
public function __construct(RequestException $e, $message = '') { $prefix = $message ?: "Bad response from the WIU API"; if ($e->getCode()) { $prefix .= " (HTTP status {$e->getCode()})"; } $response = $e->getResponse(); $body = $response->json(); $message = isset($body['message']) ? $body['message'] : $response->getReasonPhrase(); parent::__construct("{$prefix}: {$message}", $e->getCode(), null); }
/** * Captch known exceptions * @param RequestException $e * @throws BolException * @throws \Exception */ public function handle(RequestException $e) { $response = $e->getResponse(); $statusCode = $response->getStatusCode(); $this->handleStatusCode($statusCode); $body = $response->getBody(true); $message = $this->xmlParser->parse($body); if (isset($message['ErrorCode'])) { throw new BolException($message['ErrorMessage'], $message['ErrorCode']); } throw new \Exception("Unknown error occurred. Status code: {$response->getStatusCode()}."); }
/** * Wraps an API exception in the appropriate domain exception. * * @param RequestException $e The API exception * * @return HttpException */ public static function wrap(RequestException $e) { $response = $e->getResponse(); if ($errors = self::errors($response)) { $class = self::exceptionClass($response, $errors[0]); $message = implode(', ', array_map('strval', $errors)); } else { $class = self::exceptionClass($response); $message = $e->getMessage(); } return new $class($message, $errors, $e->getRequest(), $response, $e); }
/** * @param RequestException $exception * * @return ApnsException */ private static function factoryException(RequestException $exception) { $response = $exception->getResponse(); if (null === $response) { return new ApnsException('Unknown network error', 0, $exception); } try { $contents = $response->getBody()->getContents(); } catch (\Exception $e) { return new ApnsException('Unknown network error', 0, $e); } return ExceptionFactory::factoryException($response->getStatusCode(), $contents, $exception); }
public function __construct(RequestException $e) { $response = $e->getResponse(); $message = $response->getReasonPhrase() . " [status code] " . $response->getStatusCode(); $level = floor($response->getStatusCode() / 100); // Check if business-level error message // https://developer.zendesk.com/rest_api/docs/core/introduction#requests if ($level == '4') { $responseBody = $response->getBody()->getContents(); $this->errorDetails = $responseBody; $message .= ' [details] ' . $this->errorDetails; } elseif ($level == '5') { $message .= ' [details] Zendesk may be experiencing internal issues or undergoing scheduled maintenance.'; } parent::__construct($message, $response->getStatusCode()); }
public function __construct(RequestException $e) { $response = $e->getResponse(); $message = $response->getReasonPhrase(); $level = floor($response->getStatusCode() / 100); // Check if business-level error message // https://developer.zendesk.com/rest_api/docs/core/introduction#requests if ($response->getHeaderLine('Content-Type') == 'application/json; charset=UTF-8') { $responseBody = json_decode($response->getBody()->getContents()); $this->errorDetails = $responseBody->details; $message = $responseBody->description . "\n" . 'Errors: ' . print_r($this->errorDetails, true); } elseif ($level == '5') { $message = 'Zendesk may be experiencing internal issues or undergoing scheduled maintenance.'; } parent::__construct($message, $response->getStatusCode()); }
public function __construct(RequestException $e) { $message = $e->getMessage(); if ($e instanceof ClientException) { $response = $e->getResponse(); $responseBody = $response->getBody()->getContents(); $this->errorDetails = $responseBody; $message .= ' [details] ' . $this->errorDetails; } elseif ($e instanceof ServerException) { $message .= ' [details] Zendesk may be experiencing internal issues or undergoing scheduled maintenance.'; } elseif (!$e->hasResponse()) { $request = $e->getRequest(); // Unsuccessful response, log what we can $message .= ' [url] ' . $request->getUri(); $message .= ' [http method] ' . $request->getMethod(); } parent::__construct($message, $e->getCode()); }
public function __construct(RequestException $e) { $this->request = $e->getRequest(); $this->response = $e->getResponse(); $simpleMessage = $e->getMessage(); $code = 0; if ($this->response) { try { $decodedJson = Utils::jsonDecode((string) $this->response->getBody(), true); if ($decodedJson && isset($decodedJson['errorType'])) { $simpleMessage = $decodedJson['errorType'] . ' ' . $decodedJson['message']; } } catch (\InvalidArgumentException $e) { // Not Json } $code = $this->response->getStatusCode(); } $responseDescription = $this->response ? (string) $this->response : 'none'; $requestDescription = $this->request ? (string) $this->request : 'none'; $message = sprintf("%s\n\nRequest: %s\n\nResponse: %s\n\n", $simpleMessage, $requestDescription, $responseDescription); parent::__construct($message, $code, $e); }
/** * Gives human readable error message * @param RequestException $requestException * @return string */ protected function getPrettyErrorMessage(RequestException $requestException) { if (!$requestException->hasResponse()) { throw $requestException; } $errorMessage = ''; $response = json_decode($requestException->getResponse()->getBody()->getContents(), true); if (isset($response['message'])) { $errorMessage .= $response['message']; $errorMessage .= $this->getDetailedErrorMessage($response); } if (isset($response['error_description'])) { $errorMessage .= $response['error_description']; } if (isset($response['error'])) { $errorMessage .= ' [' . $response['error'] . ']'; } if (isset($response['code'])) { $errorMessage .= ' code: ' . $response['code']; } return $errorMessage; }
protected function handleException(RequestException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); if ($statusCode >= 500) { throw new KaskusServerException(); } try { $error = $response->json(); } catch (\RuntimeException $e) { throw new KaskusServerException(); } if (isset($error['errormessage'])) { $errorMessage = $error['errormessage']; if ($statusCode === 401) { throw new UnauthorizedException($errorMessage); } elseif ($statusCode === 404) { throw new ResourceNotFoundException(); } throw new KaskusClientException($errorMessage); } throw new KaskusServerException(); }
public function __construct(\GuzzleHttp\Exception\RequestException $exception) { parent::__construct($exception->getMessage(), $exception->getRequest(), $exception->getResponse(), $exception->getPrevious(), $exception->getHandlerContext()); }
/** * Prepares an array of important exception parts based on composition of a * given exception. * * @param RequestException $requestException * * @return array */ private function getRequestExceptionParts(RequestException $requestException) { $response = $requestException->getResponse(); $parts = []; $parts['reason'] = $response ? $response->getReasonPhrase() : $requestException->getMessage(); $parts['code'] = $response ? $response->getStatusCode() : $requestException->getCode(); $parts['body'] = $response ? $response->getBody() : null; return $parts; }
/** * Create the request exception * * @param Request $request * @param RequestException|null $previousException * @throws \Tmdb\Exception\TmdbApiException */ protected function handleRequestException(Request $request, RequestException $previousException) { if (null !== $previousException && null == $previousException->getResponse()) { throw new NullResponseException($this->request, $previousException); } throw $this->createApiException($request, $this->createResponse($previousException->getResponse()), $previousException); }
/** * Method will elaborate on RequestException. * * @param RequestException $e * * @throws SalesforceException * @throws TokenExpiredException */ private function assignExceptions(RequestException $e) { if ($e->hasResponse() && $e->getResponse()->getStatusCode() == 401) { throw new TokenExpiredException('Salesforce token has expired', $e); } elseif ($e->hasResponse()) { throw new SalesforceException('Salesforce response error', $e); } else { throw new SalesforceException('Invalid request: %s', $e); } }
protected function createException(GuzzleRequestException $e) { $statusCode = $e->getResponse()->getStatusCode(); switch ($statusCode) { case 401: $authInfo = $this->parseAuthHeader($e->getResponse()->getHeader('www-authenticate')); $code = array_key_exists('error', $authInfo) ? $authInfo['error'] : 401; $message = array_key_exists('error_description', $authInfo) ? $authInfo['error_description'] : $e->getMessage(); return new UnauthorizedException($message, $code, $e); default: return new ApiRequestException('Api Request failed: ' . $e->getMessage(), 0, $e); } }
/** * Handles exceptions for requests, specifically unauthorized access. * * @param RequestException $e * @throws CachetAuthenticationException */ private function handleException(RequestException $e) { if ($e->hasResponse()) { // Unauthorized if ($e->getResponse()->getStatusCode() == 401) { throw new CachetAuthenticationException(); } } // Push it up the stack. throw $e; }
/** * @param RequestException $e * * @return ClientError|ServerError */ private function createRequestException(RequestException $e) { $response = $e->getResponse(); $responseBody = $response->getBody()->getContents(); try { $status = $this->serializer->deserialize($responseBody, Status::class, 'json'); } catch (\RuntimeException $serializerException) { $status = new Status(Status::FAILURE, $responseBody); } $exceptionClass = $e instanceof ClientException ? ClientError::class : ServerError::class; return new $exceptionClass($status, $e->getRequest()); }
/** * @param RequestException $exception * * @return string */ private function create401ErrorMessage(RequestException $exception) { $request = $exception->getRequest(); $response = $exception->getResponse(); return $this->getExceptionMessage($request, $response); }
private function handleRequestException(RequestException $ex) { if ($ex->hasResponse()) { $response = $ex->getResponse(); $code = $response->getStatusCode(); $reason = $response->getReasonPhrase(); $msg = $this->getSolrErrorMessage($response); if ($msg !== null) { throw new SolrException("Solr returned HTTP {$code} {$reason}: {$msg}", 0, $ex); } else { throw new SolrException("Solr returned HTTP {$code} {$reason}", 0, $ex); } } throw new SolrException("Solr query failed", 0, $ex); }
/** * Handle Guzzle exception. * * @param \GuzzleHttp\Exception\RequestException $exception * * @throws \Hoy\Caspeco\Exceptions\HttpException */ protected function handleException(RequestException $exception) { $code = $exception->getResponse()->getStatusCode(); $message = $exception->getResponse()->getBody()->getContents(); if (Stringy::create($message)->isJson()) { throw new HttpException($exception->getResponse()->getStatusCode(), $this->formatJsonError($message)); } if (Stringy::create($message)->contains('html')) { throw new HtmlException($exception->getResponse()->getStatusCode(), $message); } throw new AuthenticationException($code, $message); }
/** * @param RequestException $e * @return ApiException */ private function apiException(RequestException $e) { $message = $e->hasResponse() ? $e->getResponse()->getBody()->getContents() : $e->getMessage(); $code = $e->hasResponse() ? $e->getResponse()->getStatusCode() : null; return new ApiException($message, $code); }
/** * Handles a Request Exception. * * @param RequestException $e The request exception. * * @return void */ protected function onRequestException(RequestException $e) { $request = $e->getRequest(); $response = $e->getResponse(); $statusCode = $response->getStatusCode(); $isClientError = $response->isClientError(); $isServerError = $response->isServerError(); if ($isClientError || $isServerError) { $content = $response->getContent(); $error = $response->getError(); $description = $response->getErrorDescription(); if (400 === $statusCode) { throw new BadRequestException($description, $error, $statusCode, $response, $request); } if (401 === $statusCode) { $otp = (string) $response->getHeader('X-Bitreserve-OTP'); if ('required' === $otp) { $description = 'Two factor authentication is enabled on this account'; throw new TwoFactorAuthenticationRequiredException($description, $error, $statusCode, $response, $request); } throw new AuthenticationRequiredException($description, $error, $statusCode, $response, $request); } if (404 === $statusCode) { $description = sprintf('Object or route not found: %s', $request->getPath()); throw new NotFoundException($description, 'not_found', $statusCode, $response, $request); } if (429 === $statusCode) { $rateLimit = $response->getApiRateLimit(); $description = sprintf('You have reached Bitreserve API limit. API limit is: %s. Your remaining requests will be reset at %s.', $rateLimit['limit'], date('Y-m-d H:i:s', $rateLimit['reset'])); throw new ApiLimitExceedException($description, $error, $statusCode, $response, $request); } throw new RuntimeException($description, $error, $statusCode, $response, $request); } }