Skip to content

Commit 555b3b6

Browse files
authored
Merge pull request #88 from annuh/fix-87-getMessagesDescriptionsAndCodes-for-single-error
fix(#87): allow getMessagesDescriptionsAndCodes for single Error
2 parents 1a1dc6c + 04d8ec5 commit 555b3b6

4 files changed

Lines changed: 61 additions & 2 deletions

File tree

src/Exception/CifException.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public function __construct($message = '', int $code = 0, \Throwable $previous =
6464
$message .= ' (' . $this->messages[0]['description'] . ')';
6565
}
6666
$code = $this->messages[0]['code'];
67+
} else {
68+
$this->messages = [
69+
[
70+
'message' => $message,
71+
'description' => $message,
72+
'code' => $code,
73+
],
74+
];
6775
}
6876

6977
parent::__construct(message: $message, code: $code, previous: $previous);

src/Service/ResponseProcessor/Rest/AbstractRestResponseProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected function validateResponse(ResponseInterface $response): bool
109109
}
110110
throw new CifException(message: $exceptionData);
111111
} elseif (!empty($body->Error)) {
112-
throw new CifException(message: (string) $body->Error->ErrorDescription, code: (int) $body->Error->ErrorCode);
112+
throw new CifException(message: (string) ($body->Error->ErrorMessage ?? ''), code: (int) ($body->Error->ErrorCode ?? 0));
113113
} elseif (!empty($body->Array->Item->ErrorMsg)) {
114114
// {"Array":{"Item":{"ErrorMsg":"Unknown option GetDeliveryDate.Options='DayTime' specified","ErrorNumber":26}}}
115115
$exceptionData = [
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Firstred\PostNL\Tests\Exception;
6+
7+
use Firstred\PostNL\Exception\CifException;
8+
use Monolog\Test\TestCase;
9+
use PHPUnit\Framework\Attributes\TestDox;
10+
11+
#[TestDox(text: 'The CifException object')]
12+
class CifExceptionTest extends TestCase
13+
{
14+
#[TestDox(text: 'Test creating a CifException with a single error')]
15+
public function testSingleError(): void
16+
{
17+
$exception = new CifException(message: 'A test exception', code: 400);
18+
19+
$this->assertEquals(expected: 'A test exception', actual: $exception->getMessage());
20+
$this->assertEquals(expected: 400, actual: $exception->getCode());
21+
$this->assertEquals(expected: [
22+
[
23+
'message' => 'A test exception',
24+
'description' => 'A test exception',
25+
'code' => 400,
26+
],
27+
], actual: $exception->getMessagesDescriptionsAndCodes());
28+
}
29+
30+
#[TestDox(text: 'Test creating a CifException with multiple errors')]
31+
public function testMultipleError(): void
32+
{
33+
$exceptionData = [
34+
[
35+
'message' => 'First exception',
36+
'description' => 'First description',
37+
'code' => 400,
38+
],
39+
[
40+
'message' => 'Second exception',
41+
'description' => 'First description',
42+
'code' => 401,
43+
],
44+
];
45+
$exception = new CifException(message: $exceptionData, code: 0);
46+
47+
$this->assertEquals(expected: 'First exception (First description)', actual: $exception->getMessage());
48+
$this->assertEquals(expected: 400, actual: $exception->getCode());
49+
$this->assertEquals(expected: $exceptionData, actual: $exception->getMessagesDescriptionsAndCodes());
50+
}
51+
}

tests/Resources/responses/rest/location/nearestlocationsbypostcode-error400.http

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Connection: keep-alive
1313
"Date": "2019-08-24T14:15:22Z",
1414
"Error": {
1515
"ErrorCode": "3000",
16-
"ErrorDescription": "Request format is invalid"
16+
"ErrorMessage": "Request format is invalid"
1717
},
1818
"RequestId": "09fd61fe-0099-4349-b71d-dce5c2472be9"
1919
}

0 commit comments

Comments
 (0)