/** * Simple exception factory for creating Intercom standardised exceptions * * @param RequestInterface $request The Request * @param Response $response The response * @return BadResponseException */ public static function factory(RequestInterface $request, Response $response) { $response_json = $response->json(); $intercom_unavailable_error = NULL; if (!static::isValidIntercomError($response_json)) { if ($response->isServerError()) { $label = 'Server error response'; $class = __NAMESPACE__ . '\\ServerErrorResponseException'; $intercom_unavailable_error = 'Service Unavailable: Back-end server is at capacity'; } else { $label = 'Unsuccessful response'; $class = __CLASS__; } } elseif ($response->isClientError()) { $label = 'Client error response'; $class = __NAMESPACE__ . '\\ClientErrorResponseException'; } elseif ($response->isServerError()) { $label = 'Server error response'; $class = __NAMESPACE__ . '\\ServerErrorResponseException'; } else { $label = 'Unsuccessful response'; $class = __CLASS__; } $message = $label . PHP_EOL . implode(PHP_EOL, array('[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[url] ' . $request->getUrl())); $e = new $class($message); $e->setResponse($response); $e->setRequest($request); // Sets the errors if the error response is the standard Intercom error type if (static::isValidIntercomError($response_json)) { $e->setErrors($response_json['errors']); } elseif ($intercom_unavailable_error != NULL) { $e->setErrors(array('code' => 'service_unavailable', "message" => $intercom_unavailable_error)); } return $e; }
/** * Simple exception factory for creating Intercom standardised exceptions * * @param RequestInterface $request The Request * @param Response $response The response * @return BadResponseException */ public static function factory(RequestInterface $request, Response $response) { if (!static::isValidIntercomError($response->json())) { $label = 'Unsuccessful response'; $class = __CLASS__; } elseif ($response->isClientError()) { $label = 'Client error response'; $class = __NAMESPACE__ . '\\ClientErrorResponseException'; } elseif ($response->isServerError()) { $label = 'Server error response'; $class = __NAMESPACE__ . '\\ServerErrorResponseException'; } else { $label = 'Unsuccessful response'; $class = __CLASS__; } $message = $label . PHP_EOL . implode(PHP_EOL, array('[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[url] ' . $request->getUrl())); $e = new $class($message); $e->setResponse($response); $e->setRequest($request); // Sets the errors if the error response is the standard Intercom error type if (static::isValidIntercomError($response->json())) { $e->setErrors($response->json()['errors']); } return $e; }
/** * @param Response $response * @return int */ private function getPriorityFromResponse(Response $response) { if ($response->isServerError() || $response->getInfo('total_time') > 5) { return Logger::ERROR; } if ($response->isClientError() || $response->getInfo('total_time') > 1) { return Logger::WARNING; } return Logger::INFO; }
/** * Factory method to create a new response exception based on the response code. * * @param RequestInterface $request Request * @param Response $response Response received * @param string $label * * @return BadResponseException */ public static function factory(RequestInterface $request, Response $response, $label = null) { if (!$label) { if ($response->isClientError()) { $label = 'Client error response'; } elseif ($response->isServerError()) { $label = 'Server error response'; } else { $label = 'Unsuccessful response'; } } $message = $label . PHP_EOL . implode(PHP_EOL, array('[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[url] ' . $request->getUrl(), '[content type] ' . $response->getContentType(), '[response body] ' . $response->getBody(true))); $result = new static($message); $result->setResponse($response); $result->setRequest($request); return $result; }
public static function factory(RequestInterface $request, Response $response) { if ($response->isClientError()) { $label = 'Client error response'; $class = __NAMESPACE__ . '\\ClientErrorResponseException'; } elseif ($response->isServerError()) { $label = 'Server error response'; $class = __NAMESPACE__ . '\\ServerErrorResponseException'; } else { $label = 'Unsuccessful response'; $class = __CLASS__; } $message = $label . PHP_EOL . implode(PHP_EOL, array('[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[url] ' . $request->getUrl())); $e = new self($message); $e->setResponse($response); $e->setRequest($request); return $e; }
/** * @covers Guzzle\Http\Message\Response::isServerError */ public function testIsServerError() { $response = new Response(500); $this->assertTrue($response->isServerError()); $response = new Response(400); $this->assertFalse($response->isServerError()); }
/** * {@inheritdoc} */ public function isServerError() { return $this->response->isServerError(); }