From 3cf4c5daddd7b7a104ac9719a8225a51088f67d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Michael=20O=2E=20Hegg=C3=B8?= Date: Tue, 1 Aug 2017 10:48:33 +0200 Subject: [PATCH] Don't throw ClientException on 4XX Some SRU servers return 4XX whenever there is a diagnostic message, including zero results responses. We want to handle those responses by our Response class. --- src/Client.php | 9 +++++++-- tests/ClientTest.php | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Client.php b/src/Client.php index d853347..c5f75f7 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,6 +4,7 @@ use Http\Client\Common\PluginClient; use Http\Client\HttpClient; use Http\Discovery\HttpClientDiscovery; +use Http\Client\Common\Exception\ServerErrorException; use Http\Client\Common\Plugin\ErrorPlugin; use Http\Discovery\MessageFactoryDiscovery; use Http\Message\Authentication\BasicAuth; @@ -33,7 +34,7 @@ class Client protected $userAgent; /** @var array Custom headers */ - protected $headers; + public $headers; /** * @var string|string[] Proxy configuration details. @@ -66,7 +67,7 @@ public function __construct( $this->url = $url; $options = $options ?: array(); - $plugins = [new ErrorPlugin()]; + $plugins = []; $this->schema = isset($options['schema']) ? $options['schema'] @@ -214,6 +215,10 @@ public function request($method, $url) $request = $this->messageFactory->createRequest($method, $url, $this->headers); $response = $this->httpClient->sendRequest($request); + if ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) { + throw new ServerErrorException($response->getReasonPhrase(), $request, $response); + } + return (string) $response->getBody(); } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 7989805..0f0b412 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -143,7 +143,7 @@ public function testHttpHeaders() 'user-agent' => 'Blablabla/0.1', )); - $opts = $sru1->getHttpHeaders(); + $opts = $sru1->headers; $this->assertEquals('application/xml', $opts['Accept']); $this->assertEquals('Blablabla/0.1', $opts['User-Agent']);