3
namespace GuzzleHttp\Subscriber;
5
use GuzzleHttp\Event\CompleteEvent;
6
use GuzzleHttp\Event\RequestEvents;
7
use GuzzleHttp\Event\SubscriberInterface;
8
use GuzzleHttp\Exception\RequestException;
11
* Throws exceptions when a 4xx or 5xx response is received
13
class HttpError implements SubscriberInterface
15
public function getEvents()
17
return ['complete' => ['onComplete', RequestEvents::VERIFY_RESPONSE]];
21
* Throw a RequestException on an HTTP protocol error
23
* @param CompleteEvent $event Emitted event
24
* @throws RequestException
26
public function onComplete(CompleteEvent $event)
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());