/** * @param ClientException $exception * @return AuthException */ private function convertException($exception) { $authException = new AuthException($exception->getResponse()->getReasonPhrase(), $exception->getCode()); $authException->setUrl($exception->getResponse()->getEffectiveUrl()); $authException->setErrors(json_decode($exception->getResponse()->getBody()->getContents())); return $authException; }
/** * Parses a ``ClientException`` for any specific exceptions thrown by the * API in the response body. If the response body is not in JSON format, * an ``Exception`` is returned. * * Check a ``ClientException`` to see if it has an associated * ``ResponseInterface`` object. * * @param ClientException $exception * @return Exception */ protected function resolveExceptionClass(ClientException $exception) { $response = $exception->getResponse()->getBody(); $response = json_decode($response->getContents()); if ($response === null) { return new Exception($exception->getMessage()); } $meta = isset($response->meta) ? $response->meta : $response; $class = '\\Larabros\\Elogram\\Exceptions\\' . $meta->error_type; return new $class($meta->error_message); }
public function handleHttpClientException(HttpClientException $e) { switch ($e->getResponse()->getStatusCode()) { case self::CONFLICT: throw new ProxyExistsException($e->getResponse()->getBody(), $e->getCode(), $e); case self::NOT_FOUND: throw new NotFoundException($e->getResponse()->getBody(), $e->getCode(), $e); case self::BAD_REQUEST: throw new InvalidToxicException($e->getResponse()->getBody(), $e->getCode(), $e); default: throw $e; } }
/** * Create an ApiException from a client exception. * * @param ClientException $e The client exception. * @return ApiException * @throws \Speicher210\KontaktIO\Exception\ApiKeyInvalidException */ protected function createApiException(ClientException $e) { $response = $e->getResponse(); if (in_array($response->getStatusCode(), array(401, 403), true)) { throw new ApiKeyInvalidException($response); } if ($response->getBody()->getSize() > 0) { /** @var ApiErrorResponse $apiErrorResponse */ $apiErrorResponse = $this->serializer->deserialize($e->getResponse()->getBody(), ApiErrorResponse::class, 'json'); } else { $apiErrorResponse = new ApiErrorResponse(); $apiErrorResponse->setStatus($response->getStatusCode()); $apiErrorResponse->setMessage($response->getReasonPhrase()); } return new ApiException($apiErrorResponse, $e); }
/** * Handle client error exceptions * * @param \GuzzleHttp\Exception\ClientException $exc * @throws \WalmartApiClient\Exception\ApiForbiddenException * @throws \WalmartApiClient\Exception\ApiNotFoundException * @throws \WalmartApiClient\Exception\ApiRequestUriTooLongException * @throws \WalmartApiClient\Exception\ApiBadRequestException */ protected function handleClientException(\GuzzleHttp\Exception\ClientException $exc) { if ($exc->hasResponse() && isset(json_decode($exc->getResponse()->getBody(), true)['errors'])) { $exception = array_shift(json_decode($exc->getResponse()->getBody(), true)['errors']); switch ($exception['code']) { case 403: throw new \WalmartApiClient\Exception\ApiForbiddenException($exception['message'], $exception['code']); case 404: throw new \WalmartApiClient\Exception\ApiNotFoundException($exception['message'], $exception['code']); case 414: throw new \WalmartApiClient\Exception\ApiRequestUriTooLongException($exception['message'], $exception['code']); default: throw new \WalmartApiClient\Exception\ApiBadRequestException($exception['message'], $exception['code']); } } throw new \WalmartApiClient\Exception\ApiBadRequestException('Bad Request', 400); }
/** * Create an ApiException from a client exception. * * @param ClientException $e The client exception. * @return ApiException */ protected function createApiException(ClientException $e) { $response = $e->getResponse(); if ($response->getStatusCode() === 401 || $response->getStatusCode() === 403) { throw new ApiKeyInvalidException(); } throw new ApiException((string) $response->getBody()); }
private function prepareException(ClientException $ce) { $message = 'An error occured'; $body = json_decode($ce->getResponse()->getBody()); if (!$body) { return $message; } return $body->message; }
/** * @param ClientException $e * * @return AbstractRateLimitException */ protected function handleRateLimitException(ClientException $e) { if ($e->getResponse()->getHeader(self::HEADER_RATE_LIMIT_TYPE)[0] === self::RATE_LIMIT_TYPE_USER) { $exception = new UserRateLimitException(); } else { $exception = new ServiceRateLimitException(); } return $exception->setClientException($e)->setRetryAfter($e->getResponse()->getHeader(self::HEADER_RETRY_AFTER)[0]); }
/** * Constructor. * * @param \GuzzleHttp\Exception\ClientException $exception * @return void * @throws \Cartalyst\Stripe\Exception\StripeException */ public function __construct(ClientException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); $error = json_decode($response->getBody(true), true)['error']; $errorCode = isset($error['code']) ? $error['code'] : null; $errorType = isset($error['type']) ? $error['type'] : null; $message = isset($error['message']) ? $error['message'] : null; $missingParameter = isset($error['param']) ? $error['param'] : null; $this->handleException($message, $statusCode, $errorType, $errorCode, $missingParameter); }
/** * @param ClientException $baseException * @return SalesforceRestApiException */ public function create(ClientException $baseException) { $message = ''; $errorResponse = json_decode($baseException->getResponse()->getBody()->getContents(), true); if (isset($errorResponse['error'])) { $message = $errorResponse['error_description']; } elseif (isset($errorResponse[0])) { $message = $errorResponse[0]['message']; } return new SalesforceRestApiException($message, 400); }
function it_maps_declined_response_402_to_payment_result_object(ClientException $exception, Response $response, ResultObjectMapperInterface $resultMapper, MethodInterface $method) { $result = ['error' => 'Error string']; $resultObject = new Payment(); $response->getStatusCode()->willReturn(402); $response->json()->willReturn($result); $method->createResultObject()->willReturn($resultObject); $exception->getResponse()->willReturn($response); $resultMapper->map($result, $resultObject)->shouldBeCalled()->willReturn($resultObject); $this->get($exception, $method)->getResult()->shouldReturn($resultObject); }
private function throwException(ClientException $e, $entity_name) { $error_data = $e->getResponse()->json(); switch ($error_data["error"]) { case 'invalid_resource': $exception = new ResourceInvalidException("Algunos campos son incorrectos en " . $entity_name); $exception->error_messages = $error_data["messages"]; throw $exception; break; default: throw new ResourceInvalidException("Petición fallida"); } }
/** * Parse an exception and return its body's error message. * * @param ClientException $guzzleException * * @return string */ protected function parseErrorMessage(ClientException $guzzleException) { $response = $guzzleException->getResponse(); if ($this->isJsonResponse($response)) { $array = $response->json(); if (isset($array['message'])) { return $array['message']; } if (isset($array['error_description'])) { return $array['error_description']; } } return (string) $response->xml()->message; }
function __construct(ClientException $badResponseException) { $message = $badResponseException->message; $response = $badResponseException->getResponse()->getBody(); $response = json_decode($response); //Add rev data to the message for easy debugging if ($response && is_object($response)) { if (isset($response->code, $response->message)) { $this->rev_code = $response->code; $this->rev_message = $response->message; } $message .= PHP_EOL; foreach (get_object_vars($response) as $key => $value) { $message .= '[rev ' . $key . '] ' . $value . PHP_EOL; } } parent::__construct($message, $badResponseException->code, $badResponseException); }
/** * Parse an exception and return its body's error message. * * @param ClientException $guzzleException * * @return string */ protected function parseErrorMessage(ClientException $guzzleException) { $response = $guzzleException->getResponse(); if ($this->isJsonResponse($response)) { $array = $response->json(); if (isset($array['message'])) { return $array['message']; } if (isset($array['error_description'])) { return $array['error_description']; } } try { return (string) $response->xml()->message; } catch (\Exception $ex) { return 'Excpetion while processing error response. Error reason phrase: ' . $response->getReasonPhrase(); } }
/** * @param ClientException $clientException * * @return ApiException | SearchLimitException */ public static function createThrowable(ClientException $clientException) { $data = \GuzzleHttp\json_decode($clientException->getResponse()->getBody()->getContents()); $code = 0; $message = ''; if (!empty($data->error)) { $error = $data->error; if (!empty($error->code)) { $code = $error->code; } if (!empty($error->message)) { $message = $error->message; } } if ($code == SearchLimitException::EXCEPTION_CODE) { return new SearchLimitException($message); } return new ApiException($message, $code); }
/** * @param ClientException $exception * @return \Exception */ protected function resolveExceptionClass(ClientException $exception) { $response = $exception->getResponse()->getBody(); $response = json_decode($response->getContents()); if ($response === null) { return new \Exception($exception->getMessage()); } if (!empty($response->message)) { $message = (string) $response->message; } elseif (!empty($response->errors)) { $message = $response->errors; if (is_array($response->errors)) { $message = []; foreach ($response->errors as $error) { $message[] = $error->message; } $message = implode(PHP_EOL, $message); } } else { $message = (string) $response; } return new \Exception($message); }
/** * Handle http client exceptions * * @param HttpClientException $e * * @return void * @throws Exception */ private function handleRequestException(HttpClientException $e) { if ($response = $e->getResponse()) { $exception = new Exception($response->getReasonPhrase(), $response->getStatusCode(), $e); $exception->setBody(json_decode($response->getBody())); throw $exception; } throw new Exception($e->getMessage(), 500, $e); }
/** * @param ClientException $e * @param $code int|array prepare body by response status */ protected static function prepareExeptionMessage(ClientException $e, $code) { self::$data = new \stdClass(); self::$data->message = null; self::$data->reason = null; $codes = !is_array($code) ? [$code] : $code; if (in_array($e->getResponse()->getStatusCode(), $codes)) { $response_body = Json::decode($e->getResponse()->getBody(), false); if ($response_body) { self::$data->message = isset($response_body->data->error_message) ? $response_body->data->error_message : null; if (isset($response_body->data->error_code, self::$errors[$response_body->data->error_code])) { self::$data->reason = self::$errors[$response_body->data->error_code]; } } } }
/** * Parse the client error and return the error message thrown. * @param ClientException $e * @return string */ private function parseClientError(ClientException $e) { $msg = ""; if ($e->getResponse() != null && $e->getResponse()->getBody() != null) { $json = json_decode($e->getResponse()->getBody()->getContents(), true); $msg = isset($json['error']) ? $json['error'] : ""; } return $msg; }
/** * @param HttpClientException $exception * @throws TransportException */ protected function handleClientException(HttpClientException $exception) { switch ($exception->getResponse()->getStatusCode()) { case 401: throw new UnauthorizedException("Unauthorized API request."); default: throw new ClientException($exception->getMessage()); } }
public function __construct(ClientException $exception) { $data = $exception->getResponse()->json(); return parent::__construct($data['error']); }
/** * @param ClientException $e * @throws Exception */ private function exceptionHasResponse(ClientException $e) { if ($e->hasResponse()) { $json = (string) $e->getResponse()->getBody(); $output = json_decode($json, true); if (empty($output['error'])) { throw new Exception("An error response was returned but the content could not be parsed: {$json}"); } $exception_message = []; foreach ($output['error'] as $field => $error_message) { $exception_message[] = "For field '{$field}' the API reported: {$error_message}"; } throw new Exception(implode("\n", $exception_message)); } }
public static function create(\GuzzleHttp\Exception\ClientException $e) { $message = sprintf('Request could not be processed (%s %s): %s on %s', $e->getResponse()->getStatusCode(), $e->getResponse()->getReasonPhrase(), $e->getMessage(), $e->getRequest()->getUri()); return new self($message, $e->getResponse()->getStatusCode(), $e); }
/** * Show error. * @param ClientException $ce * @param OutputInterface $output */ protected function showError(ClientException $ce, OutputInterface $output) { if ($ce->getResponse()->getStatusCode() == 422) { $output->writeln('<error>Repository already exists</error>'); die; } $output->writeln('<error>Client Request exception thrown</error>'); die; }
/** * this is called when exception is thrown on sending request to API Server * @param ClientException $ce * @throws \Exception */ public function isRequestForBidden(ClientException $ce) { $this->response = $ce->getResponse(); $this->validateLastRequestStatus(); }
/** * @param \GuzzleHttp\Exception\ClientException $e * @return StorageApiEvent */ public function clientError(\GuzzleHttp\Exception\ClientException $e) { $response = null; try { $response = ResponseDecoder::decode($e->getResponse()); } catch (\Exception $jsonE) { $response = $e->getResponse()->getBody(); } $event = $this->prepareEvent(); $event->setMessage(sprintf(self::MESSAGE_END, $this->task->getRunUrl()))->setDescription('Error response from component')->setType(StorageApiEvent::TYPE_WARN)->setResults(array('response' => $response, 'code' => $e->getResponse()->getStatusCode())); $this->save($event); return $event; }
/** * Output a clear explanation for domains API errors. * * @param ClientException $e * @param Project $project * * @throws ClientException If it can't be explained. */ protected function handleApiException(ClientException $e, Project $project) { $response = $e->getResponse(); if ($response !== null && $response->getStatusCode() === 403) { $project->ensureFull(); $data = $project->getData(); if (!$project->hasLink('#manage-domains') && !empty($data['subscription']['plan']) && $data['subscription']['plan'] === 'development') { $this->stdErr->writeln('This project is on a Development plan. Upgrade the plan to add domains.'); } } else { throw $e; } }
/** * Turns a ClientException into a TiketException - like magic. * @param ClientException $exception - Guzzle ClientException * @return TiketException */ protected function convertException($exception) { $tiketException = new TiketException($exception->getResponse()->getReasonPhrase(), $exception->getCode()); $tiketException->setUrl($exception->getResponse()->getEffectiveUrl()); $tiketException->setErrors(json_decode($exception->getResponse()->getBody()->getContents())); return $tiketException; }
public function handleClientException(ClientException $e, $data = []) { $contents = json_decode($e->getResponse()->getBody()->getContents()); $exception = new RemoteException($e->getMessage()); $exception->data = $data; $exception->headers = $e->getRequest()->getHeaders(); $exception->setErrorCode(isset($contents->errors) ? $contents->errors[0]->code : null); $exception->setError(isset($contents->errors) ? $contents->errors[0]->description : ''); throw $exception; }