質問編集履歴

1

追記

2019/03/29 08:07

投稿

Sfidante
Sfidante

スコア90

test CHANGED
File without changes
test CHANGED
@@ -58,6 +58,448 @@
58
58
 
59
59
 
60
60
 
61
+ RequestExceptionは以下のとおりです。
62
+
63
+ ```php
64
+
65
+ <?php
66
+
67
+ namespace GuzzleHttp\Exception;
68
+
69
+
70
+
71
+ use Psr\Http\Message\RequestInterface;
72
+
73
+ use Psr\Http\Message\ResponseInterface;
74
+
75
+ use GuzzleHttp\Promise\PromiseInterface;
76
+
77
+ use Psr\Http\Message\UriInterface;
78
+
79
+
80
+
81
+ /**
82
+
83
+ * HTTP Request exception
84
+
85
+ */
86
+
87
+ class RequestException extends TransferException
88
+
89
+ {
90
+
91
+ /** @var RequestInterface */
92
+
93
+ private $request;
94
+
95
+
96
+
97
+ /** @var ResponseInterface */
98
+
99
+ private $response;
100
+
101
+
102
+
103
+ /** @var array */
104
+
105
+ private $handlerContext;
106
+
107
+
108
+
109
+ public function __construct(
110
+
111
+ $message,
112
+
113
+ RequestInterface $request,
114
+
115
+ ResponseInterface $response = null,
116
+
117
+ \Exception $previous = null,
118
+
119
+ array $handlerContext = []
120
+
121
+ ) {
122
+
123
+ // Set the code of the exception if the response is set and not future.
124
+
125
+ $code = $response && !($response instanceof PromiseInterface)
126
+
127
+ ? $response->getStatusCode()
128
+
129
+ : 0;
130
+
131
+ parent::__construct($message, $code, $previous);
132
+
133
+ $this->request = $request;
134
+
135
+ $this->response = $response;
136
+
137
+ $this->handlerContext = $handlerContext;
138
+
139
+ }
140
+
141
+
142
+
143
+ /**
144
+
145
+ * Wrap non-RequestExceptions with a RequestException
146
+
147
+ *
148
+
149
+ * @param RequestInterface $request
150
+
151
+ * @param \Exception $e
152
+
153
+ *
154
+
155
+ * @return RequestException
156
+
157
+ */
158
+
159
+ public static function wrapException(RequestInterface $request, \Exception $e)
160
+
161
+ {
162
+
163
+ return $e instanceof RequestException
164
+
165
+ ? $e
166
+
167
+ : new RequestException($e->getMessage(), $request, null, $e);
168
+
169
+ }
170
+
171
+
172
+
173
+ /**
174
+
175
+ * Factory method to create a new exception with a normalized error message
176
+
177
+ *
178
+
179
+ * @param RequestInterface $request Request
180
+
181
+ * @param ResponseInterface $response Response received
182
+
183
+ * @param \Exception $previous Previous exception
184
+
185
+ * @param array $ctx Optional handler context.
186
+
187
+ *
188
+
189
+ * @return self
190
+
191
+ */
192
+
193
+ public static function create(
194
+
195
+ RequestInterface $request,
196
+
197
+ ResponseInterface $response = null,
198
+
199
+ \Exception $previous = null,
200
+
201
+ array $ctx = []
202
+
203
+ ) {
204
+
205
+ if (!$response) {
206
+
207
+ return new self(
208
+
209
+ 'Error completing request',
210
+
211
+ $request,
212
+
213
+ null,
214
+
215
+ $previous,
216
+
217
+ $ctx
218
+
219
+ );
220
+
221
+ }
222
+
223
+
224
+
225
+ $level = (int) floor($response->getStatusCode() / 100);
226
+
227
+ if ($level === 4) {
228
+
229
+ $label = 'Client error';
230
+
231
+ $className = ClientException::class;
232
+
233
+ } elseif ($level === 5) {
234
+
235
+ $label = 'Server error';
236
+
237
+ $className = ServerException::class;
238
+
239
+ } else {
240
+
241
+ $label = 'Unsuccessful request';
242
+
243
+ $className = __CLASS__;
244
+
245
+ }
246
+
247
+
248
+
249
+ $uri = $request->getUri();
250
+
251
+ $uri = static::obfuscateUri($uri);
252
+
253
+
254
+
255
+ // Client Error: `GET /` resulted in a `404 Not Found` response:
256
+
257
+ // <html> ... (truncated)
258
+
259
+ $message = sprintf(
260
+
261
+ '%s: `%s %s` resulted in a `%s %s` response',
262
+
263
+ $label,
264
+
265
+ $request->getMethod(),
266
+
267
+ $uri,
268
+
269
+ $response->getStatusCode(),
270
+
271
+ $response->getReasonPhrase()
272
+
273
+ );
274
+
275
+
276
+
277
+ $summary = static::getResponseBodySummary($response);
278
+
279
+
280
+
281
+ if ($summary !== null) {
282
+
283
+ $message .= ":\n{$summary}\n";
284
+
285
+ }
286
+
287
+
288
+
289
+ return new $className($message, $request, $response, $previous, $ctx);
290
+
291
+ }
292
+
293
+
294
+
295
+ /**
296
+
297
+ * Get a short summary of the response
298
+
299
+ *
300
+
301
+ * Will return `null` if the response is not printable.
302
+
303
+ *
304
+
305
+ * @param ResponseInterface $response
306
+
307
+ *
308
+
309
+ * @return string|null
310
+
311
+ */
312
+
313
+ public static function getResponseBodySummary(ResponseInterface $response)
314
+
315
+ {
316
+
317
+ $body = $response->getBody();
318
+
319
+
320
+
321
+ if (!$body->isSeekable() || !$body->isReadable()) {
322
+
323
+ return null;
324
+
325
+ }
326
+
327
+
328
+
329
+ $size = $body->getSize();
330
+
331
+
332
+
333
+ if ($size === 0) {
334
+
335
+ return null;
336
+
337
+ }
338
+
339
+
340
+
341
+ $summary = $body->read(120);
342
+
343
+ $body->rewind();
344
+
345
+
346
+
347
+ if ($size > 120) {
348
+
349
+ $summary .= ' (truncated...)';
350
+
351
+ }
352
+
353
+
354
+
355
+ // Matches any printable character, including unicode characters:
356
+
357
+ // letters, marks, numbers, punctuation, spacing, and separators.
358
+
359
+ if (preg_match('/[^\pL\pM\pN\pP\pS\pZ\n\r\t]/', $summary)) {
360
+
361
+ return null;
362
+
363
+ }
364
+
365
+
366
+
367
+ return $summary;
368
+
369
+ }
370
+
371
+
372
+
373
+ /**
374
+
375
+ * Obfuscates URI if there is an username and a password present
376
+
377
+ *
378
+
379
+ * @param UriInterface $uri
380
+
381
+ *
382
+
383
+ * @return UriInterface
384
+
385
+ */
386
+
387
+ private static function obfuscateUri($uri)
388
+
389
+ {
390
+
391
+ $userInfo = $uri->getUserInfo();
392
+
393
+
394
+
395
+ if (false !== ($pos = strpos($userInfo, ':'))) {
396
+
397
+ return $uri->withUserInfo(substr($userInfo, 0, $pos), '***');
398
+
399
+ }
400
+
401
+
402
+
403
+ return $uri;
404
+
405
+ }
406
+
407
+
408
+
409
+ /**
410
+
411
+ * Get the request that caused the exception
412
+
413
+ *
414
+
415
+ * @return RequestInterface
416
+
417
+ */
418
+
419
+ public function getRequest()
420
+
421
+ {
422
+
423
+ return $this->request;
424
+
425
+ }
426
+
427
+
428
+
429
+ /**
430
+
431
+ * Get the associated response
432
+
433
+ *
434
+
435
+ * @return ResponseInterface|null
436
+
437
+ */
438
+
439
+ public function getResponse()
440
+
441
+ {
442
+
443
+ return $this->response;
444
+
445
+ }
446
+
447
+
448
+
449
+ /**
450
+
451
+ * Check if a response was received
452
+
453
+ *
454
+
455
+ * @return bool
456
+
457
+ */
458
+
459
+ public function hasResponse()
460
+
461
+ {
462
+
463
+ return $this->response !== null;
464
+
465
+ }
466
+
467
+
468
+
469
+ /**
470
+
471
+ * Get contextual information about the error from the underlying handler.
472
+
473
+ *
474
+
475
+ * The contents of this array will vary depending on which handler you are
476
+
477
+ * using. It may also be just an empty array. Relying on this data will
478
+
479
+ * couple you to a specific handler, but can give more debug information
480
+
481
+ * when needed.
482
+
483
+ *
484
+
485
+ * @return array
486
+
487
+ */
488
+
489
+ public function getHandlerContext()
490
+
491
+ {
492
+
493
+ return $this->handlerContext;
494
+
495
+ }
496
+
497
+ }
498
+
499
+ ```
500
+
501
+
502
+
61
503
  ※ 現在は、Apache等でアクセス制御はかけていないです
62
504
 
63
505