/** * @param BadResponseException $previous * @return XsollaAPIException */ public static function fromBadResponse(BadResponseException $previous) { $statusCode = $previous->getResponse()->getStatusCode(); $message = sprintf(static::$messageTemplate, $previous->getMessage(), $previous->getRequest(), $previous->getResponse()); if (array_key_exists($statusCode, static::$exceptions)) { return new static::$exceptions[$statusCode]($message, 0, $previous); } return new self($message, 0, $previous); }
public static function fromBadResponseException(BadResponseException $old) { $new = new self($old->getMessage()); $new->setRequest($old->getRequest()); $new->setResponse($old->getResponse()); return $new; }
protected function handleBadResponse(BadResponseException $e) { if ($e->getResponse()->getStatusCode() == 404) { $this->storage->delete($e->getRequest()); throw $e; } }
/** * @covers Guzzle\Http\Exception\BadResponseException */ public function testBadResponseException() { $e = new BadResponseException('Message'); $response = new Response(200); $e->setResponse($response); $this->assertEquals($response, $e->getResponse()); }
/** * @param BadResponseException error * * @return string */ public function getErrorMessage(BadResponseException $error) { $jsonResponse = json_decode($error->getResponse()->getBody(), true); $errorMessage = $error->getMessage(); if (count($jsonResponse['errors']) > 0) { $errorMessage = $jsonResponse['errors'][0]['message']; } return $errorMessage; }
/** * Handles a bad response when attempting to revalidate * * @param BadResponseException $e Exception encountered * * @throws BadResponseException */ protected 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; } }
public static function factory(BadResponseException $exception) { $response = $exception->getResponse(); $message = sprintf("This operation was forbidden; the API returned a %s status code with this message:\n%s", $response->getStatusCode(), (string) $response->getBody()); $e = new self($message); $e->setResponse($response); $e->setRequest($exception->getRequest()); return $e; }
public static function factory(BadResponseException $exception) { $response = $exception->getResponse(); $message = sprintf("This resource you were looking for could not be found; the API returned a %s status code with this message:\n%s", $response->getStatusCode(), (string) $response->getBody()); $e = new self($message); $e->setResponse($response); $e->setRequest($exception->getRequest()); return $e; }
/** * @param BadResponseException $cause * * @return self */ public static function factory(BadResponseException $cause) { $request = $cause->getRequest(); $response = $cause->getResponse(); if ($response->isClientError()) { $class = __NAMESPACE__ . '\\ClientFailureException'; } else { $class = __NAMESPACE__ . '\\ServerFailureException'; } $exception = new $class($response->getReasonPhrase(), null, $cause); if ($exception instanceof HttpException) { $statusCode = $response->getStatusCode(); $httpMethod = $request->getMethod(); $exception->setStatusCode($statusCode); $exception->setResponse($response); $exception->setRequest($request); if (isset(self::$descriptionMapping[$httpMethod][$statusCode])) { $exception->setDescription(self::$descriptionMapping[$httpMethod][$statusCode]); } } return $exception; }
/** * Handle a bad response coming back when getting token credentials. * * @param BadResponseException $e * @return void * @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."); }
private function handleBadResponseExceptions(BadResponseException $exception) { $response = $exception->getResponse(); $json = $response->json(); throw new RuntimeException(sprintf('Request to Dropbox failed: %s [%d]', $json['error'], $response->getStatusCode())); }
/** * Throw specific Pusher exceptions according to the status code * * @param BadResponseException $exception * @throws Exception\UnauthorizedException * @throws Exception\ForbiddenException * @throws Exception\RuntimeException * @throws Exception\UnknownResourceException * @return void */ protected function handleException(BadResponseException $exception) { $response = $exception->getResponse(); if ($response->isSuccessful()) { return; } // Reason is injected into the body content, however we do not really want to output the whole // body content to the user, but rather only the real reason, which is typically the last line // of the body content $body = array_filter(explode(PHP_EOL, $response->getMessage())); $message = end($body); switch ($response->getStatusCode()) { case 400: throw new Exception\RuntimeException(sprintf('An error occurred while trying to handle your request. Reason: %s', $message), 400); case 401: throw new Exception\UnauthorizedException(sprintf('You are not authorized to perform this action. Reason: %s', $message), 401); case 403: throw new Exception\ForbiddenException(sprintf('You are not allowed to perform this action, your application may be disabled or you may have reached your message quota. Reason: %s', $message), 403); case 404: throw new Exception\UnknownResourceException(sprintf('Resource cannot be found, are you sure it exists? Reason: %s', $message), 404); default: throw new Exception\RuntimeException(sprintf('An unknown error occurred on Pusher side. Reason: %s', $message), $response->getStatusCode()); } }
protected function buildException(GuzzleEx\BadResponseException $exception) { $response = $exception->getResponse(); $body = $response->getBody(true); $message = $body; $code = null; if ($response->isContentType('application/json')) { $data = @json_decode($body, true); $message = isset($data['message']) ? $data['message'] : '(no error message given)'; if (isset($data['status'])) { $status = (int) $data['status']; $code = $status - (int) ($status / 1000) * 1000; } } switch ($response->getStatusCode()) { case 400: return new SymfonyEx\BadRequestHttpException($message, null, $code); case 401: case 403: return new SymfonyEx\AccessDeniedHttpException($message, null, $code); case 404: return new SymfonyEx\NotFoundHttpException($message, null, $code); case 405: return new SymfonyEx\MethodNotAllowedHttpException(array(), $message, null, $code); case 406: return new SymfonyEx\NotAcceptableHttpException($message, null, $code); case 409: return new SymfonyEx\ConflictHttpException($message, null, $code); case 415: return new SymfonyEx\UnsupportedMediaTypeHttpException($message, null, $code); case 500: case 503: return new SymfonyEx\ServiceUnavailableHttpException(null, $message, null, $code); default: return new SymfonyEx\HttpException(500, $message, null, array(), $code); } }
private function handleBadResponseExceptions(BadResponseException $exception) { $response = $exception->getResponse(); $json = $response->json(); $message = isset($json['error']['message']) ? $json['error']['message'] : $json['error']; throw new RuntimeException(sprintf('Request to Google Drive failed: %s [%d]', $message, $response->getStatusCode())); }
/** * Handle an error response exception / object * @param BadResponseException $exception * @return ErrorException $error_exception */ protected function handleErrorResponse(BadResponseException $exception) { $response = Response::create($exception->getResponse()); $errorException = new ErrorException('An error occurred on this request', 0, $exception); $errorException->setResponse($response); $contentType = $response->getHttpHeader('Content-Type'); if (!empty($contentType)) { $errorClass = $this->getErrorDocumentClass($contentType); if (!is_null($errorClass)) { $errorDocument = $errorClass::createFromString($response->getBody()); $errorException->setErrorDocument($errorDocument); } } return $errorException; }