source: externals/php/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php@ 851179e

Last change on this file since 851179e was 01a620f, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Removed submodules for php externals and use composer to provide them and versioned them afterwards

  • Property mode set to 100644
File size: 3.9 KB
Line 
1<?php
2namespace GuzzleHttp\Exception;
3
4use Psr\Http\Message\RequestInterface;
5use Psr\Http\Message\ResponseInterface;
6use GuzzleHttp\Promise\PromiseInterface;
7
8/**
9 * HTTP Request exception
10 */
11class RequestException extends TransferException
12{
13 /** @var RequestInterface */
14 private $request;
15
16 /** @var ResponseInterface */
17 private $response;
18
19 /** @var array */
20 private $handlerContext;
21
22 public function __construct(
23 $message,
24 RequestInterface $request,
25 ResponseInterface $response = null,
26 \Exception $previous = null,
27 array $handlerContext = []
28 ) {
29 // Set the code of the exception if the response is set and not future.
30 $code = $response && !($response instanceof PromiseInterface)
31 ? $response->getStatusCode()
32 : 0;
33 parent::__construct($message, $code, $previous);
34 $this->request = $request;
35 $this->response = $response;
36 $this->handlerContext = $handlerContext;
37 }
38
39 /**
40 * Wrap non-RequestExceptions with a RequestException
41 *
42 * @param RequestInterface $request
43 * @param \Exception $e
44 *
45 * @return RequestException
46 */
47 public static function wrapException(RequestInterface $request, \Exception $e)
48 {
49 return $e instanceof RequestException
50 ? $e
51 : new RequestException($e->getMessage(), $request, null, $e);
52 }
53
54 /**
55 * Factory method to create a new exception with a normalized error message
56 *
57 * @param RequestInterface $request Request
58 * @param ResponseInterface $response Response received
59 * @param \Exception $previous Previous exception
60 * @param array $ctx Optional handler context.
61 *
62 * @return self
63 */
64 public static function create(
65 RequestInterface $request,
66 ResponseInterface $response = null,
67 \Exception $previous = null,
68 array $ctx = []
69 ) {
70 if (!$response) {
71 return new self(
72 'Error completing request',
73 $request,
74 null,
75 $previous,
76 $ctx
77 );
78 }
79
80 $level = floor($response->getStatusCode() / 100);
81 if ($level == '4') {
82 $label = 'Client error response';
83 $className = __NAMESPACE__ . '\\ClientException';
84 } elseif ($level == '5') {
85 $label = 'Server error response';
86 $className = __NAMESPACE__ . '\\ServerException';
87 } else {
88 $label = 'Unsuccessful response';
89 $className = __CLASS__;
90 }
91
92 $message = $label . ' [url] ' . $request->getUri()
93 . ' [http method] ' . $request->getMethod()
94 . ' [status code] ' . $response->getStatusCode()
95 . ' [reason phrase] ' . $response->getReasonPhrase();
96
97 return new $className($message, $request, $response, $previous, $ctx);
98 }
99
100 /**
101 * Get the request that caused the exception
102 *
103 * @return RequestInterface
104 */
105 public function getRequest()
106 {
107 return $this->request;
108 }
109
110 /**
111 * Get the associated response
112 *
113 * @return ResponseInterface|null
114 */
115 public function getResponse()
116 {
117 return $this->response;
118 }
119
120 /**
121 * Check if a response was received
122 *
123 * @return bool
124 */
125 public function hasResponse()
126 {
127 return $this->response !== null;
128 }
129
130 /**
131 * Get contextual information about the error from the underlying handler.
132 *
133 * The contents of this array will vary depending on which handler you are
134 * using. It may also be just an empty array. Relying on this data will
135 * couple you to a specific handler, but can give more debug information
136 * when needed.
137 *
138 * @return array
139 */
140 public function getHandlerContext()
141 {
142 return $this->handlerContext;
143 }
144}
Note: See TracBrowser for help on using the repository browser.