Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#87): allow getMessagesDescriptionsAndCodes for single Error #88

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Exception/CifException.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public function __construct($message = '', int $code = 0, \Throwable $previous =
$message .= ' (' . $this->messages[0]['description'] . ')';
}
$code = $this->messages[0]['code'];
} else {
$this->messages = [
[
'message' => $message,
'description' => $message,
'code' => $code,
],
];
}

parent::__construct(message: $message, code: $code, previous: $previous);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected function validateResponse(ResponseInterface $response): bool
}
throw new CifException(message: $exceptionData);
} elseif (!empty($body->Error)) {
throw new CifException(message: (string) $body->Error->ErrorDescription, code: (int) $body->Error->ErrorCode);
throw new CifException(message: (string) ($body->Error->ErrorMessage ?? ''), code: (int) ($body->Error->ErrorCode ?? 0));
} elseif (!empty($body->Array->Item->ErrorMsg)) {
// {"Array":{"Item":{"ErrorMsg":"Unknown option GetDeliveryDate.Options='DayTime' specified","ErrorNumber":26}}}
$exceptionData = [
Expand Down
51 changes: 51 additions & 0 deletions tests/Exception/CifExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Firstred\PostNL\Tests\Exception;

use Firstred\PostNL\Exception\CifException;
use Monolog\Test\TestCase;
use PHPUnit\Framework\Attributes\TestDox;

#[TestDox(text: 'The CifException object')]
class CifExceptionTest extends TestCase
{
#[TestDox(text: 'Test creating a CifException with a single error')]
public function testSingleError(): void
{
$exception = new CifException(message: 'A test exception', code: 400);

$this->assertEquals(expected: 'A test exception', actual: $exception->getMessage());
$this->assertEquals(expected: 400, actual: $exception->getCode());
$this->assertEquals(expected: [
[
'message' => 'A test exception',
'description' => 'A test exception',
'code' => 400,
],
], actual: $exception->getMessagesDescriptionsAndCodes());
}

#[TestDox(text: 'Test creating a CifException with multiple errors')]
public function testMultipleError(): void
{
$exceptionData = [
[
'message' => 'First exception',
'description' => 'First description',
'code' => 400,
],
[
'message' => 'Second exception',
'description' => 'First description',
'code' => 401,
],
];
$exception = new CifException(message: $exceptionData, code: 0);

$this->assertEquals(expected: 'First exception (First description)', actual: $exception->getMessage());
$this->assertEquals(expected: 400, actual: $exception->getCode());
$this->assertEquals(expected: $exceptionData, actual: $exception->getMessagesDescriptionsAndCodes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Connection: keep-alive
"Date": "2019-08-24T14:15:22Z",
"Error": {
"ErrorCode": "3000",
"ErrorDescription": "Request format is invalid"
"ErrorMessage": "Request format is invalid"
},
"RequestId": "09fd61fe-0099-4349-b71d-dce5c2472be9"
}
Loading