Skip to content

Commit

Permalink
fix json Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
chengxs committed Mar 20, 2024
1 parent 5171278 commit 5700209
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Exceptions/UnserializableResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@

final class UnserializableResponse extends Exception
{
protected string $content;
/**
* Creates a new Exception instance.
*/
public function __construct(JsonException $exception)
{
parent::__construct($exception->getMessage(), 0, $exception);
}

public function getContent(): string
{
return $this->content;
}

public function setContent(string $content): void
{
$this->content = $content;
}
}
4 changes: 3 additions & 1 deletion src/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public function requestObject(Payload $payload): Response
/** @var array{error?: array{message: string, type: string, code: string}} $data */
$data = json_decode($contents, true, flags: JSON_THROW_ON_ERROR);
} catch (JsonException $jsonException) {
throw new UnserializableResponse($jsonException);
$unserializableResponse = new UnserializableResponse($jsonException);
$unserializableResponse->setContent($contents);
throw $unserializableResponse;
}

return Response::from($data, $response->getHeaders());
Expand Down

0 comments on commit 5700209

Please sign in to comment.