Skip to content

Commit 9bdbea2

Browse files
authored
Merge pull request #86 from annuh/issue-82-handle-error-response
fix(#82): handle errors responses with a single `Error`
2 parents e56eea9 + 292b3ae commit 9bdbea2

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

src/Service/ResponseProcessor/Rest/AbstractRestResponseProcessor.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ protected function validateResponse(ResponseInterface $response): bool
108108
}
109109
}
110110
throw new CifException(message: $exceptionData);
111+
} elseif (!empty($body->Error)) {
112+
throw new CifException(message: (string) $body->Error->ErrorDescription, code: (int) $body->Error->ErrorCode);
111113
} elseif (!empty($body->Array->Item->ErrorMsg)) {
112114
// {"Array":{"Item":{"ErrorMsg":"Unknown option GetDeliveryDate.Options='DayTime' specified","ErrorNumber":26}}}
113115
$exceptionData = [
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
HTTP/1.1 400 OK
2+
Cache-Control: private, max-age=0
3+
Content-Type: application/json;charset=UTF-8
4+
Date: Wed, 31 Mar 2021 12:42:12 GMT
5+
Strict-Transport-Security: max-age=31536000
6+
X-Content-Type-Options: nosniff
7+
X-Frame-Options: SAMEORIGIN
8+
X-Robots-Tag: noindex
9+
X-XSS-Protection: 1; mode=block
10+
Connection: keep-alive
11+
12+
{
13+
"Date": "2019-08-24T14:15:22Z",
14+
"Error": {
15+
"ErrorCode": "3000",
16+
"ErrorDescription": "Request format is invalid"
17+
},
18+
"RequestId": "09fd61fe-0099-4349-b71d-dce5c2472be9"
19+
}

tests/Service/LocationServiceTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use Firstred\PostNL\Entity\Response\GetLocationsInAreaResponse;
4242
use Firstred\PostNL\Entity\Response\GetNearestLocationsResponse;
4343
use Firstred\PostNL\Entity\Response\ResponseLocation;
44+
use Firstred\PostNL\Exception\CifException;
4445
use Firstred\PostNL\HttpClient\MockHttpClient;
4546
use Firstred\PostNL\PostNL;
4647
use Firstred\PostNL\Service\LocationServiceInterface;
@@ -56,7 +57,9 @@
5657
use Psr\Http\Message\RequestInterface;
5758
use Psr\Http\Message\ResponseInterface;
5859
use ReflectionObject;
60+
5961
use function file_get_contents;
62+
6063
use const _RESPONSES_DIR_;
6164

6265
/**
@@ -320,6 +323,37 @@ public function testGetLocationRest($response)
320323
$this->assertEquals(expected: '161503', actual: $result->getLocationCode());
321324
}
322325

326+
/** @throws */
327+
#[TestDox(text: 'can handle HTTP 400 error response correctly')]
328+
public function testHttp400Error(): void
329+
{
330+
$response = PsrMessage::parseResponse(message: file_get_contents(filename: _RESPONSES_DIR_.'/rest/location/nearestlocationsbypostcode-error400.http'));
331+
332+
$mock = new MockHandler(queue: [$response]);
333+
$handler = HandlerStack::create(handler: $mock);
334+
$mockClient = new MockHttpClient();
335+
$mockClient->setHandler(handler: $handler);
336+
$this->postnl->setHttpClient(httpClient: $mockClient);
337+
338+
$this->expectException(exception: CifException::class);
339+
$this->expectExceptionMessage(message: 'Request format is invalid');
340+
341+
$this->postnl->getNearestLocations(getNearestLocations: (new GetNearestLocations())
342+
->setCountrycode(Countrycode: 'NL')
343+
->setLocation(Location: new Location(
344+
Postalcode: '0000AB',
345+
AllowSundaySorting: true,
346+
DeliveryDate: '29-06-2016',
347+
DeliveryOptions: ['PG', 'PGE'],
348+
OpeningTime: '09:00:00',
349+
Options: ['Daytime'],
350+
City: 'Hoofddorp',
351+
Street: 'Siriusdreef',
352+
HouseNr: '42',
353+
HouseNrExt: 'A',
354+
)));
355+
}
356+
323357
/**
324358
* @return array[]
325359
*/

0 commit comments

Comments
 (0)