Skip to content

Commit

Permalink
Improve update client errors handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Sep 2, 2024
1 parent c558924 commit 0ade2e0
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Client/ActiveCampaignResourceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,22 @@ public function update(int $activeCampaignResourceId, ResourceInterface $resourc
$serializedResource,
));
if (($statusCode = $response->getStatusCode()) !== 200) {
if ($statusCode === 404) {
/** @var array{message: string} $errorResponse */
$errorResponse = json_decode($response->getBody()->getContents(), true, 512, \JSON_THROW_ON_ERROR);
switch ($statusCode) {
case 404:
/** @var array{message: string} $errorResponse */
$errorResponse = json_decode($response->getBody()->getContents(), true, 512, \JSON_THROW_ON_ERROR);

throw new NotFoundHttpException($errorResponse['message']);
}
throw new NotFoundHttpException($errorResponse['message']);
case 422:
/** @var array{errors: array{title: string, detail: string, code: string, source: array{pointer: string}}} $errorResponse */
$errorResponse = json_decode($response->getBody()->getContents(), true, 512, \JSON_THROW_ON_ERROR);
/** @var string[] $titles */
$titles = array_column($errorResponse['errors'], 'title');

throw new HttpException($statusCode, $response->getReasonPhrase(), null, $response->getHeaders());
throw new UnprocessableEntityHttpException(implode('; ', $titles));
default:
throw new HttpException($statusCode, $response->getReasonPhrase(), null, $response->getHeaders());
}
}

/** @var UpdateResourceResponseInterface $updateResourceResponse */
Expand Down

0 comments on commit 0ade2e0

Please sign in to comment.