Skip to content

Commit 8cb0b98

Browse files
committed
Fix exceptions
1 parent de6a88a commit 8cb0b98

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

templates/php/src/Client.php.twig

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ class Client
120120
* @return array|string
121121
* @throws {{spec.title | caseUcfirst}}Exception
122122
*/
123-
public function call($method, $path = '', $headers = array(), array $params = array(), ?string $responseType = null)
123+
public function call(
124+
string $method,
125+
string $path = '',
126+
array $headers = [],
127+
array $params = [],
128+
?string $responseType = null
129+
)
124130
{
125131
$headers = array_merge($this->headers, $headers);
126132
$ch = curl_init($this->endpoint . $path . (($method == self::METHOD_GET && !empty($params)) ? '?' . http_build_query($params) : ''));
@@ -191,14 +197,14 @@ class Client
191197
}
192198
193199
if (curl_errno($ch)) {
194-
throw new {{spec.title | caseUcfirst}}Exception(curl_error($ch), $responseStatus, $responseBody);
200+
throw new {{spec.title | caseUcfirst}}Exception(curl_error($ch), $responseStatus, $responseBody['type'] ?? '', $responseBody);
195201
}
196202
197203
curl_close($ch);
198204
199205
if($responseStatus >= 400) {
200206
if(is_array($responseBody)) {
201-
throw new {{spec.title | caseUcfirst}}Exception($responseBody['message'], $responseStatus, $responseBody['type'] ?? '', $responseBody);
207+
throw new {{spec.title | caseUcfirst}}Exception($responseBody['message'], $responseStatus, $responseBody['type'] ?? '', json_encode($responseBody));
202208
} else {
203209
throw new {{spec.title | caseUcfirst}}Exception($responseBody, $responseStatus);
204210
}

templates/php/src/Exception.php.twig

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,25 @@ class {{spec.title | caseUcfirst}}Exception extends Exception {
99
/**
1010
* @var mixed
1111
*/
12-
private $response;
12+
private ?string $response;
1313
1414
/**
1515
* @var string
1616
*/
17-
private $type;
17+
private string $type;
1818
1919
/**
20-
* @param String $message
20+
* @param ?string $message
2121
* @param int $code
22-
* @param mixed $response
22+
* @param string $type
23+
* @param ?string $response
2324
*/
24-
public function __construct($message = null, $code = 0, $type = null, $response = null)
25-
{
25+
public function __construct(
26+
?string $message = null,
27+
int $code = 0,
28+
string $type = '',
29+
?string $response = null
30+
) {
2631
parent::__construct($message, $code);
2732
$this->response = $response;
2833
$this->type = $type;
@@ -31,15 +36,15 @@ class {{spec.title | caseUcfirst}}Exception extends Exception {
3136
/**
3237
* @return string
3338
*/
34-
public function getType()
39+
public function getType(): string
3540
{
3641
return $this->type;
3742
}
3843
3944
/**
40-
* @return mixed
45+
* @return ?string
4146
*/
42-
final public function getResponse()
47+
final public function getResponse(): ?string
4348
{
4449
return $this->response;
4550
}

0 commit comments

Comments
 (0)