1: <?php
2:
3: namespace Guzzle\Http\Exception;
4:
5: use Guzzle\Http\Message\RequestInterface;
6: use Guzzle\Http\Message\Response;
7:
8: /**
9: * Http request exception thrown when a bad response is received
10: */
11: class BadResponseException extends RequestException
12: {
13: /**
14: * @var Response
15: */
16: private $response;
17:
18: /**
19: * Factory method to create a new response exception based on the response code.
20: *
21: * @param RequestInterface $request Request
22: * @param Response $response Response received
23: *
24: * @return BadResponseException
25: */
26: public static function factory(RequestInterface $request, Response $response)
27: {
28: if ($response->isClientError()) {
29: $label = 'Client error response';
30: $class = __NAMESPACE__ . '\\ClientErrorResponseException';
31: } elseif ($response->isServerError()) {
32: $label = 'Server error response';
33: $class = __NAMESPACE__ . '\\ServerErrorResponseException';
34: } else {
35: $label = 'Unsuccessful response';
36: $class = __CLASS__;
37: $e = new self();
38: }
39:
40: $message = $label . PHP_EOL . implode(PHP_EOL, array(
41: '[status code] ' . $response->getStatusCode(),
42: '[reason phrase] ' . $response->getReasonPhrase(),
43: '[url] ' . $request->getUrl(),
44: ));
45:
46: $e = new $class($message);
47: $e->setResponse($response);
48: $e->setRequest($request);
49:
50: return $e;
51: }
52:
53: /**
54: * Set the response that caused the exception
55: *
56: * @param Response $response Response to set
57: */
58: public function setResponse(Response $response)
59: {
60: $this->response = $response;
61: }
62:
63: /**
64: * Get the response that caused the exception
65: *
66: * @return Response
67: */
68: public function getResponse()
69: {
70: return $this->response;
71: }
72: }
73: