scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace GuzzleHttp\Exception;
4  
5 use GuzzleHttp\Message\RequestInterface;
6 use GuzzleHttp\Message\ResponseInterface;
7  
8 /**
9 * HTTP Request exception
10 */
11 class RequestException extends TransferException
12 {
13 /** @var bool */
14 private $emittedErrorEvent = false;
15  
16 /** @var RequestInterface */
17 private $request;
18  
19 /** @var ResponseInterface */
20 private $response;
21  
22 /** @var bool */
23 private $throwImmediately = false;
24  
25 public function __construct(
26 $message,
27 RequestInterface $request,
28 ResponseInterface $response = null,
29 \Exception $previous = null
30 ) {
31 $code = $response ? $response->getStatusCode() : 0;
32 parent::__construct($message, $code, $previous);
33 $this->request = $request;
34 $this->response = $response;
35 }
36  
37 /**
38 * Factory method to create a new exception with a normalized error message
39 *
40 * @param RequestInterface $request Request
41 * @param ResponseInterface $response Response received
42 * @param \Exception $previous Previous exception
43 *
44 * @return self
45 */
46 public static function create(
47 RequestInterface $request,
48 ResponseInterface $response = null,
49 \Exception $previous = null
50 ) {
51 if (!$response) {
52 return new self('Error completing request', $request, null, $previous);
53 }
54  
55 $level = $response->getStatusCode()[0];
56 if ($level == '4') {
57 $label = 'Client error response';
58 $className = __NAMESPACE__ . '\\ClientException';
59 } elseif ($level == '5') {
60 $label = 'Server error response';
61 $className = __NAMESPACE__ . '\\ServerException';
62 } else {
63 $label = 'Unsuccessful response';
64 $className = __CLASS__;
65 }
66  
67 $message = $label . ' [url] ' . $request->getUrl()
68 . ' [status code] ' . $response->getStatusCode()
69 . ' [reason phrase] ' . $response->getReasonPhrase();
70  
71 return new $className($message, $request, $response, $previous);
72 }
73  
74 /**
75 * Get the request that caused the exception
76 *
77 * @return RequestInterface
78 */
79 public function getRequest()
80 {
81 return $this->request;
82 }
83  
84 /**
85 * Get the associated response
86 *
87 * @return ResponseInterface|null
88 */
89 public function getResponse()
90 {
91 return $this->response;
92 }
93  
94 /**
95 * Check if a response was received
96 *
97 * @return bool
98 */
99 public function hasResponse()
100 {
101 return $this->response !== null;
102 }
103  
104 /**
105 * Check or set if the exception was emitted in an error event.
106 *
107 * This value is used in the RequestEvents::emitBefore() method to check
108 * to see if an exception has already been emitted in an error event.
109 *
110 * @param bool|null Set to true to set the exception as having emitted an
111 * error. Leave null to retrieve the current setting.
112 *
113 * @return null|bool
114 * @throws \InvalidArgumentException if you attempt to set the value to false
115 */
116 public function emittedError($value = null)
117 {
118 if ($value === null) {
119 return $this->emittedErrorEvent;
120 } elseif ($value === true) {
121 $this->emittedErrorEvent = true;
122 } else {
123 throw new \InvalidArgumentException('You cannot set the emitted '
124 . 'error value to false.');
125 }
126 }
127  
128 /**
129 * Sets whether or not parallel adapters SHOULD throw the exception
130 * immediately rather than handling errors through asynchronous error
131 * handling.
132 *
133 * @param bool $throwImmediately
134 *
135 */
136 public function setThrowImmediately($throwImmediately)
137 {
138 $this->throwImmediately = $throwImmediately;
139 }
140  
141 /**
142 * Gets the setting specified by setThrowImmediately().
143 *
144 * @return bool
145 */
146 public function getThrowImmediately()
147 {
148 return $this->throwImmediately;
149 }
150 }