4
* (c) Copyright 2014 Rackspace US, Inc.
6
* Licensed under the Apache License, Version 2.0 (the "License"); you may
7
* not use this file except in compliance with the License. You may obtain
8
* a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
* License for the specific language governing permissions and limitations
19
namespace OpenStack\Common\Transport\Guzzle;
21
use GuzzleHttp\Event\CompleteEvent;
22
use GuzzleHttp\Event\RequestEvents;
23
use GuzzleHttp\Event\SubscriberInterface;
24
use GuzzleHttp\Subscriber\HttpError as GuzzleHttpError;
25
use OpenStack\Common\Transport\Exception\RequestException;
28
* A subscriber for capturing Guzzle's HTTP error events and processing them in
29
* a standardised manner.
31
class HttpError implements SubscriberInterface
33
public function getEvents()
35
return ['complete' => ['onComplete', RequestEvents::VERIFY_RESPONSE]];
39
* When a request completes, this method is executed. Because this class
40
* checks for HTTP errors and handles them, this method checks the HTTP
41
* status code and invokes {@see RequestException} if necessary.
43
* @param CompleteEvent $event
44
* @throws \OpenStack\Common\Transport\Exception\RequestException
46
public function onComplete(CompleteEvent $event)
48
$status = (int) $event->getResponse()->getStatusCode();
50
// Has an error occurred (4xx or 5xx status)?
51
if ($status >= 400 && $status <= 505) {
52
$request = new RequestAdapter($event->getRequest());
53
$response = new ResponseAdapter($event->getResponse());
54
throw RequestException::create($request, $response);
b'\\ No newline at end of file'