/** * 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; }
/** * @param BadResponseException $ex * @param array $response * * @throws AuthenticationException * @throws InvalidDocumentStructureException * @throws ResourceNotFoundException * @throws VersionNotFoundException */ private function handleInvalidApiUsage(BadResponseException $ex, array $response) { switch ($response['error']['errors'][0]['reason']) { case 'resource_not_found': throw new ResourceNotFoundException(sprintf('Resource not found at %s', $ex->getRequest()->getRequestTarget()), $ex->getCode()); case 'version_not_found': throw new VersionNotFoundException('Version not found', $ex->getCode()); case 'invalid_document_structure': throw new InvalidDocumentStructureException($response['error']['message'], $ex->getCode()); case 'access_token_not_found': case 'insufficient_permissions': throw new AuthenticationException($response['error']['message'], $ex->getCode()); } }
/** * @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]']]; }