/** * Handles a bad response when attempting to validate * * @param BadResponseException $e Exception encountered * * @throws BadResponseException */ private function handleBadResponse(BadResponseException $e) { // 404 errors mean the resource no longer exists, so remove from // cache, and prevent an additional request by throwing the exception if ($e->getResponse()->getStatusCode() == 404) { $this->storage->delete($e->getRequest()); throw $e; } }
/** * The standard error message from guzzle is quite poor in informations, * this will give little bit more sense to it and return it * * @param BadResponseException $e * @return BadResponseException */ protected function handleError(BadResponseException $e) { $request = $e->getRequest(); $response = $e->getResponse(); $level = floor($response->getStatusCode() / 100); if ($level == '4') { $label = 'Client error response'; } elseif ($level == '5') { $label = 'Server error response'; } else { $label = 'Unsuccessful response'; } $message = $label . ' [url] ' . $request->getUrl() . ' [status code] ' . $response->getStatusCode() . ' [message] '; try { $message .= $response->json()['message']; } catch (ParseException $e) { $message .= (string) $response->getBody(); } return new BadResponseException($message, $request, $response, $e->getPrevious()); }
/** * The standard error message from guzzle is quite poor in informations, * this will give little bit more sense to it and return it * * @param BadResponseException $e * @return BadResponseException */ protected function handleError(BadResponseException $e) { $request = $e->getRequest(); $response = $e->getResponse(); $level = floor($response->getStatusCode() / 100); if ($level == '4') { $label = 'Client error response'; } elseif ($level == '5') { $label = 'Server error response'; } else { $label = 'Unsuccessful response'; } $message = $label . ' [url] ' . $request->getUri() . ' [status code] ' . $response->getStatusCode() . ' [message] '; $body = $response->getBody(); $json = json_decode($body, true); $message .= $json != null && array_key_exists('message', $json) ? $json['message'] : $body; if ($level == '4' && strpos($message, "page with this title already exists") !== false) { return new DuplicateTitleException($message, 0, $e->getPrevious()); } return new BadResponseException($message, $request, $response, $e->getPrevious()); }
/** * Handles a bad response when attempting to validate. * * If the resource no longer exists, then remove from the cache. * * @param BadResponseException $e Exception encountered * * @throws BadResponseException */ private function handleBadResponse(BadResponseException $e) { if (isset(self::$gone[$e->getResponse()->getStatusCode()])) { $this->storage->delete($e->getRequest()); } throw $e; }
/** * Handles the error for us, just a bit of code abstraction. * * @param BadResponseException $exception * * @throws JSONError */ public static function handleError(BadResponseException $exception) { $response_body = (string) $exception->getResponse()->getBody(); $array_body = decode($response_body); $code = $exception->getResponse()->getStatusCode(); $message = null; if (isset($array_body['message'])) { $message = $array_body['message']; } elseif (isset($array_body['code'])) { $message = $array_body['code']; } throw new JSONError($message, $code, $array_body); }
/** * @param BadResponseException $ex * * @throws InvalidStateException * @throws ResourceNotFoundException * @throws ValidationException * @throws VersionNotFoundException * @throws InvalidDocumentStructureException */ private function handleBadResponseException(BadResponseException $ex) { $response = json_decode($ex->getResponse()->getBody()->getContents(), true); switch ($response['error']['type']) { case 'invalid_state': throw new InvalidStateException($response['error']['message'], $response['error']['code'], $response['error']['errors']); case 'validation_failed': $this->handleValidationFailedErrors($response); case 'invalid_api_usage': $this->handleInvalidApiUsage($ex, $response); } throw $ex; }
/** * @author WN * @param Exception\BadResponseException $e * @return array */ private function formatBadResponseException(Exception\BadResponseException $e) { return ['message' => $e->getMessage(), 'request' => ['headers' => $e->getRequest()->getHeaders(), 'body' => $e->getRequest()->getBody()->getContents(), 'method' => $e->getRequest()->getMethod(), 'uri' => $e->getRequest()->getUri()], 'response' => ['body' => $e->getResponse() ? $e->getResponse()->getBody()->getContents() : '[EMPTY]', 'headers' => $e->getResponse() ? $e->getResponse()->getHeaders() : '[EMPTY]']]; }
/** * Handle a bad response coming back when getting token credentials. * * @param BadResponseException $e * * @throws CredentialsException */ protected function handleTokenCredentialsBadResponse(BadResponseException $e) { $response = $e->getResponse(); $body = $response->getBody(); $statusCode = $response->getStatusCode(); throw new CredentialsException("Received HTTP status code [{$statusCode}] with message \"{$body}\" when getting token credentials."); }
/** * @param BadResponseException $e * @return mixed */ public static function getBadResponseResult(BadResponseException $e) { return $e->getResponse()->json(); }
/** * @param BadResponseException $e * * @return ApiException */ public static function fromBadResponseException(BadResponseException $e) { $message = 'An error has occurred during the API call'; if ($e->getResponse() instanceof ResponseInterface) { $body = (string) $e->getResponse()->getBody(); $json = json_decode($body, true); if (isset($json['message'])) { $message = $json['message']; } } return new self($message); }