diff --git a/src/Api/FingerprintApi.php b/src/Api/FingerprintApi.php index d3b451ef..fd799616 100644 --- a/src/Api/FingerprintApi.php +++ b/src/Api/FingerprintApi.php @@ -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]; @@ -174,7 +176,10 @@ public function getEvent(string $request_id): array } throw $e; - } catch (\Exception $_) { + } catch (SerializationException $exception) { + $exception->setResponse($e->getResponseObject()); + + throw $exception; } } } @@ -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]; @@ -268,7 +275,10 @@ function ($e) { } throw $e; - } catch (\Exception $_) { + } catch (SerializationException $exception) { + $exception->setResponse($e->getResponseObject()); + + throw $e; } } ); @@ -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]; @@ -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; } } } @@ -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]; @@ -472,7 +489,10 @@ function ($e) { } throw $e; - } catch (\Exception $_) { + } catch (SerializationException $exception) { + $exception->setResponse($e->getResponseObject()); + + throw $e; } } ); diff --git a/src/ObjectSerializer.php b/src/ObjectSerializer.php index 5cd215b3..adcb378a 100644 --- a/src/ObjectSerializer.php +++ b/src/ObjectSerializer.php @@ -296,7 +296,7 @@ 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); @@ -304,7 +304,7 @@ public static function deserialize(mixed $data, string $class, ?array $httpHeade 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()); diff --git a/src/SerializationException.php b/src/SerializationException.php index 9c0fe867..51049575 100644 --- a/src/SerializationException.php +++ b/src/SerializationException.php @@ -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; } diff --git a/template/api.mustache b/template/api.mustache index 796b5652..2b9556f2 100644 --- a/template/api.mustache +++ b/template/api.mustache @@ -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]; @@ -147,7 +148,9 @@ use \GuzzleHttp\Exception\GuzzleException; {{/responses}} } throw $e; - } catch (\Exception $_) { + } catch(SerializationException $exception) { + $exception->setResponse($e->getResponseObject()); + throw $exception; } } } @@ -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]; @@ -223,7 +227,9 @@ use \GuzzleHttp\Exception\GuzzleException; {{/responses}} } throw $e; - } catch (\Exception $_) { + } catch(SerializationException $exception) { + $exception->setResponse($e->getResponseObject()); + throw $e; } } );