Skip to content

Commit

Permalink
Better exception handling (#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pilskalns authored Dec 1, 2023
1 parent 888d70f commit 3dbdc2f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/Hydrator/ArrayHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ public function hydrate(ResponseInterface $response, string $class)
throw new HydrationException('The ArrayHydrator cannot hydrate response with Content-Type:'.$response->getHeaderLine('Content-Type'));
}

$content = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new HydrationException(sprintf('Error (%d) when trying to json_decode response', json_last_error()));
try {
$content = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
throw new HydrationException(sprintf('Error (%d) when trying to json_decode response: %s', $exception->getCode(), $exception->getMessage()));
}

return $content;
Expand Down
8 changes: 4 additions & 4 deletions src/Hydrator/ModelHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public function hydrate(ResponseInterface $response, string $class)
throw new HydrationException('The ModelHydrator cannot hydrate response with Content-Type: '.$contentType);
}

$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR);

if (JSON_ERROR_NONE !== json_last_error()) {
throw new HydrationException(sprintf('Error (%d) when trying to json_decode response', json_last_error()));
try {
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
throw new HydrationException(sprintf('Error (%d) when trying to json_decode response: %s', $exception->getCode(), $exception->getMessage()));
}

if (is_subclass_of($class, ApiResponse::class)) {
Expand Down

0 comments on commit 3dbdc2f

Please sign in to comment.