This commit represents a partial overhaul of error handling for requests
made against the v4 Cloudflare API, with an aim of unifying disparate
kinds of exceptions under a single `ResponseException` type, and the
covering of additional cases where errors were unhandled. Specifically:
- The `Guzzle::request()` function will now catch Guzzle exceptions
normally thrown in cases of client and server errors (4xx and 5xx)
response codes, and convert these to `ResponseException` types
before re-throwing. These types of errors were previously not caught
and were instead returned verbatim, expecting downstream clients to
be aware of internal details of how these functions operate.
- Conversely, we no longer assume that all responses are JSON-encoded,
and no longer try to derive errors from non-4xx or 5xx responses.
All public endpoints under the v4 API are expected to be
well-behaved in that regard, and never return an error response
where none is indicated in the HTTP code.
Code has been moved around and test-cases added in support of these
changes. In most cases, these changes won't break any existing
expectations and won't require any changes to downstream code, but users
of the Cloudflare SDK should ensure that they are indeed set up for
catching `ResponseException` instances thrown during requests, and
should not expect to see Guzzle exceptions directly (though these are
still available in calls to `ResponseException::getPrevious()`).
Fixes: #152
79 lines
3.2 KiB
PHP
79 lines
3.2 KiB
PHP
<?php
|
|
|
|
use Cloudflare\API\Adapter\ResponseException;
|
|
use Cloudflare\API\Adapter\JSONException;
|
|
use GuzzleHttp\Exception\RequestException;
|
|
use GuzzleHttp\Psr7\Request;
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
class ResponseExceptionTest extends TestCase
|
|
{
|
|
public function testFromRequestExceptionNoResponse()
|
|
{
|
|
$reqErr = new RequestException('foo', new Request('GET', '/test'));
|
|
$respErr = ResponseException::fromRequestException($reqErr);
|
|
|
|
$this->assertInstanceOf(ResponseException::class, $respErr);
|
|
$this->assertEquals($reqErr->getMessage(), $respErr->getMessage());
|
|
$this->assertEquals(0, $respErr->getCode());
|
|
$this->assertEquals($reqErr, $respErr->getPrevious());
|
|
}
|
|
|
|
public function testFromRequestExceptionEmptyContentType()
|
|
{
|
|
$resp = new Response(404);
|
|
$reqErr = new RequestException('foo', new Request('GET', '/test'), $resp);
|
|
$respErr = ResponseException::fromRequestException($reqErr);
|
|
|
|
$this->assertInstanceOf(ResponseException::class, $respErr);
|
|
$this->assertEquals($reqErr->getMessage(), $respErr->getMessage());
|
|
$this->assertEquals(0, $respErr->getCode());
|
|
$this->assertEquals($reqErr, $respErr->getPrevious());
|
|
}
|
|
|
|
|
|
public function testFromRequestExceptionUnknownContentType()
|
|
{
|
|
$resp = new Response(404, ['Content-Type' => ['application/octet-stream']]);
|
|
$reqErr = new RequestException('foo', new Request('GET', '/test'), $resp);
|
|
$respErr = ResponseException::fromRequestException($reqErr);
|
|
|
|
$this->assertInstanceOf(ResponseException::class, $respErr);
|
|
$this->assertEquals($reqErr->getMessage(), $respErr->getMessage());
|
|
$this->assertEquals(0, $respErr->getCode());
|
|
$this->assertEquals($reqErr, $respErr->getPrevious());
|
|
}
|
|
|
|
public function testFromRequestExceptionJSONDecodeError()
|
|
{
|
|
$resp = new Response(404, ['Content-Type' => ['application/json; charset=utf-8']], '[what]');
|
|
$reqErr = new RequestException('foo', new Request('GET', '/test'), $resp);
|
|
$respErr = ResponseException::fromRequestException($reqErr);
|
|
|
|
$this->assertInstanceOf(ResponseException::class, $respErr);
|
|
$this->assertEquals($reqErr->getMessage(), $respErr->getMessage());
|
|
$this->assertEquals(0, $respErr->getCode());
|
|
$this->assertInstanceOf(JSONException::class, $respErr->getPrevious());
|
|
$this->assertEquals($reqErr, $respErr->getPrevious()->getPrevious());
|
|
}
|
|
|
|
public function testFromRequestExceptionJSONWithErrors()
|
|
{
|
|
$body = '{
|
|
"result": null,
|
|
"success": false,
|
|
"errors": [{"code":1003, "message":"This is an error"}],
|
|
"messages": []
|
|
}';
|
|
|
|
$resp = new Response(404, ['Content-Type' => ['application/json; charset=utf-8']], $body);
|
|
$reqErr = new RequestException('foo', new Request('GET', '/test'), $resp);
|
|
$respErr = ResponseException::fromRequestException($reqErr);
|
|
|
|
$this->assertInstanceOf(ResponseException::class, $respErr);
|
|
$this->assertEquals('This is an error', $respErr->getMessage());
|
|
$this->assertEquals(1003, $respErr->getCode());
|
|
$this->assertEquals($reqErr, $respErr->getPrevious());
|
|
}
|
|
}
|