/** * 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); }
/** * @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)); } }