diff --git a/CHANGELOG.md b/CHANGELOG.md index c8225067..27a77199 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Sending API key as HTTP header instead of query param in FileRequestBuilder ([PR126](https://github.com/5pm-HDH/churchtools-api/pull/126)) - Update ChurchTools from 3.90.0 to 3.91.1 ([PR129](https://github.com/5pm-HDH/churchtools-api/pull/129)) +- Improved the formatting of ChurchTool's error response ([PR132](https://github.com/5pm-HDH/churchtools-api/pull/132)) ### Fixed diff --git a/src/Exceptions/CTRequestException.php b/src/Exceptions/CTRequestException.php index 332f026d..4bb3bf9e 100644 --- a/src/Exceptions/CTRequestException.php +++ b/src/Exceptions/CTRequestException.php @@ -100,10 +100,10 @@ public static function ofErrorResponse(ResponseInterface $response): self if (array_key_exists('args', $error) && array_key_exists('value', $error['args'])) { $value = $error['args']['value']; - if ('' === $value) { - $description .= ' If no e-mail address is available set it to null.'; + if (null === $value) { + $description .= ' Provided value was NULL.'; } else { - $description .= sprintf('Provided value was "%s".', $value); + $description .= sprintf(' Provided value was "%s".', $value); } } @@ -111,6 +111,19 @@ public static function ofErrorResponse(ResponseInterface $response): self continue; } + + if (isset($error['message'])) { + $args = isset($error['args']) && is_array($error['args']) ? $error['args'] : []; + + $placeholders = array_map(function ($key) { + return sprintf('{%s}', $key); + }, array_keys($args)); + + $description = strtr($error['message'], array_combine($placeholders, $args)); + $errorDescriptions[] = $description; + + continue; + } } $msg .= '. ' . implode(' ', $errorDescriptions);