Skip to content

Commit

Permalink
refactor: throw serialization exception from object serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Orkuncakilkaya committed Aug 4, 2024
1 parent e74f920 commit ee54f5b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
44 changes: 32 additions & 12 deletions src/Api/FingerprintApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ public function getEvent(string $request_id): array

try {
$serialized = ObjectSerializer::deserialize($responseBody, $returnType, []);
} catch (\Exception $e) {
throw new SerializationException($response, $e);
} catch (SerializationException $e) {
$e->setResponse($response);

throw $e;
}

return [$serialized, $response];
Expand Down Expand Up @@ -174,7 +176,10 @@ public function getEvent(string $request_id): array
}

throw $e;
} catch (\Exception $_) {
} catch (SerializationException $exception) {
$exception->setResponse($e->getResponseObject());

throw $exception;
}
}
}
Expand Down Expand Up @@ -215,8 +220,10 @@ function ($response) use ($returnType, $request) {

try {
$serialized = ObjectSerializer::deserialize($responseBody, $returnType, []);
} catch (\Exception $e) {
throw new SerializationException($response, $e);
} catch (SerializationException $e) {
$e->setResponse($response);

throw $e;
}

return [$serialized, $response];
Expand Down Expand Up @@ -268,7 +275,10 @@ function ($e) {
}

throw $e;
} catch (\Exception $_) {
} catch (SerializationException $exception) {
$exception->setResponse($e->getResponseObject());

throw $e;
}
}
);
Expand Down Expand Up @@ -326,8 +336,10 @@ public function getVisits(string $visitor_id, ?string $request_id = null, ?strin

try {
$serialized = ObjectSerializer::deserialize($responseBody, $returnType, []);
} catch (\Exception $e) {
throw new SerializationException($response, $e);
} catch (SerializationException $e) {
$e->setResponse($response);

throw $e;
}

return [$serialized, $response];
Expand Down Expand Up @@ -378,7 +390,10 @@ public function getVisits(string $visitor_id, ?string $request_id = null, ?strin
}

throw $e;
} catch (\Exception $_) {
} catch (SerializationException $exception) {
$exception->setResponse($e->getResponseObject());

throw $exception;
}
}
}
Expand Down Expand Up @@ -419,8 +434,10 @@ function ($response) use ($returnType, $request) {

try {
$serialized = ObjectSerializer::deserialize($responseBody, $returnType, []);
} catch (\Exception $e) {
throw new SerializationException($response, $e);
} catch (SerializationException $e) {
$e->setResponse($response);

throw $e;
}

return [$serialized, $response];
Expand Down Expand Up @@ -472,7 +489,10 @@ function ($e) {
}

throw $e;
} catch (\Exception $_) {
} catch (SerializationException $exception) {
$exception->setResponse($e->getResponseObject());

throw $e;
}
}
);
Expand Down
4 changes: 2 additions & 2 deletions src/ObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,15 @@ public static function deserialize(mixed $data, string $class, ?array $httpHeade
return (float) $originalData;
}
if ('string' === $normalizedClass && is_object($data)) {
throw new \Exception('Cannot convert object to string');
throw new SerializationException();
}

settype($data, $class);
if (gettype($data) === $normalizedClass) {
return $data;
}

throw new \Exception('Serialization error: Could not convert '.gettype($originalData).' to '.$class);
throw new SerializationException();
} elseif (method_exists($class, 'getAllowableEnumValues')) {
if (!in_array($data, $class::getAllowableEnumValues())) {
$imploded = implode("', '", $class::getAllowableEnumValues());
Expand Down
13 changes: 9 additions & 4 deletions src/SerializationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@

final class SerializationException extends \Exception
{
protected readonly ResponseInterface $response;
protected ?ResponseInterface $response;

public function __construct(ResponseInterface $response, \Exception $prev)
public function __construct(?ResponseInterface $response = null, ?\Exception $prev = null)
{
parent::__construct("Response from the server couldn't be serialized", $prev->getCode(), $prev);
parent::__construct("Response from the server couldn't be serialized", $prev ? $prev->getCode() : 0, $prev);
$this->response = $response;
}

public function getResponse(): ResponseInterface
public function setResponse(ResponseInterface $response): void
{
$this->response = $response;
}

public function getResponse(): ?ResponseInterface
{
return $this->response;
}
Expand Down
18 changes: 12 additions & 6 deletions template/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ use \GuzzleHttp\Exception\GuzzleException;

try {
$serialized = ObjectSerializer::deserialize($responseBody, $returnType, []);
} catch (\Exception $e) {
throw new SerializationException($response, $e);
} catch (SerializationException $e) {
$e->setResponse($response);
throw $e;
}

return [$serialized, $response];
Expand Down Expand Up @@ -147,7 +148,9 @@ use \GuzzleHttp\Exception\GuzzleException;
{{/responses}}
}
throw $e;
} catch (\Exception $_) {
} catch(SerializationException $exception) {
$exception->setResponse($e->getResponseObject());
throw $exception;
}
}
}
Expand Down Expand Up @@ -192,8 +195,9 @@ use \GuzzleHttp\Exception\GuzzleException;

try {
$serialized = ObjectSerializer::deserialize($responseBody, $returnType, []);
} catch (\Exception $e) {
throw new SerializationException($response, $e);
} catch (SerializationException $e) {
$e->setResponse($response);
throw $e;
}

return [$serialized, $response];
Expand Down Expand Up @@ -223,7 +227,9 @@ use \GuzzleHttp\Exception\GuzzleException;
{{/responses}}
}
throw $e;
} catch (\Exception $_) {
} catch(SerializationException $exception) {
$exception->setResponse($e->getResponseObject());
throw $e;
}
}
);
Expand Down

0 comments on commit ee54f5b

Please sign in to comment.