~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/guzzlehttp/guzzle/src/Subscriber/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
namespace GuzzleHttp\Subscriber;
 
4
 
 
5
use GuzzleHttp\Event\CompleteEvent;
 
6
use GuzzleHttp\Event\RequestEvents;
 
7
use GuzzleHttp\Event\SubscriberInterface;
 
8
use GuzzleHttp\Exception\RequestException;
 
9
 
 
10
/**
 
11
 * Throws exceptions when a 4xx or 5xx response is received
 
12
 */
 
13
class HttpError implements SubscriberInterface
 
14
{
 
15
    public function getEvents()
 
16
    {
 
17
        return ['complete' => ['onComplete', RequestEvents::VERIFY_RESPONSE]];
 
18
    }
 
19
 
 
20
    /**
 
21
     * Throw a RequestException on an HTTP protocol error
 
22
     *
 
23
     * @param CompleteEvent $event Emitted event
 
24
     * @throws RequestException
 
25
     */
 
26
    public function onComplete(CompleteEvent $event)
 
27
    {
 
28
        $code = (string) $event->getResponse()->getStatusCode();
 
29
        // Throw an exception for an unsuccessful response
 
30
        if ($code[0] === '4' || $code[0] === '5') {
 
31
            throw RequestException::create($event->getRequest(), $event->getResponse());
 
32
        }
 
33
    }
 
34
}