~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to src/OpenStack/Common/Transport/Guzzle/HttpError.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/*
 
4
 * (c) Copyright 2014 Rackspace US, Inc.
 
5
 *
 
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
 
9
 *
 
10
 *      http://www.apache.org/licenses/LICENSE-2.0
 
11
 *
 
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
 
16
 * under the License.
 
17
 */
 
18
 
 
19
namespace OpenStack\Common\Transport\Guzzle;
 
20
 
 
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;
 
26
 
 
27
/**
 
28
 * A subscriber for capturing Guzzle's HTTP error events and processing them in
 
29
 * a standardised manner.
 
30
 */
 
31
class HttpError implements SubscriberInterface
 
32
{
 
33
    public function getEvents()
 
34
    {
 
35
        return ['complete' => ['onComplete', RequestEvents::VERIFY_RESPONSE]];
 
36
    }
 
37
 
 
38
    /**
 
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.
 
42
     *
 
43
     * @param CompleteEvent $event
 
44
     * @throws \OpenStack\Common\Transport\Exception\RequestException
 
45
     */
 
46
    public function onComplete(CompleteEvent $event)
 
47
    {
 
48
        $status = (int) $event->getResponse()->getStatusCode();
 
49
 
 
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);
 
55
        }
 
56
    }
 
57
 
 
b'\\ No newline at end of file'