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

Improved the formatting of ChurchTool's error response #132

Merged
merged 2 commits into from
Jan 26, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 16 additions & 3 deletions src/Exceptions/CTRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,30 @@ 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);
}
}

$errorDescriptions[] = $description;

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);
Expand Down