Skip to content

Commit 6b7059d

Browse files
committed
Merge pull request #1394 from akkie/api-exception-json
Deserialize the response body in case of an ApiException
2 parents bff8f96 + b054cd3 commit 6b7059d

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

modules/swagger-codegen/src/main/resources/php/ApiClient.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ class ApiClient
239239
$data = $http_body;
240240
}
241241
} else {
242+
$data = json_decode($http_body);
243+
if (json_last_error() > 0) { // if response is a string
244+
$data = $http_body;
245+
}
246+
242247
throw new ApiException(
243248
"[".$response_info['http_code']."] Error connecting to the API ($url)",
244249
$response_info['http_code'], $http_header, $http_body

modules/swagger-codegen/src/main/resources/php/ApiException.mustache

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,38 +46,38 @@ use \Exception;
4646
class ApiException extends Exception
4747
{
4848
49-
/**
50-
* The HTTP body of the server response.
51-
* @var string
49+
/**
50+
* The HTTP body of the server response either as Json or string.
51+
* @var mixed
5252
*/
5353
protected $responseBody;
54-
54+
5555
/**
5656
* The HTTP header of the server response.
5757
* @var string[]
5858
*/
5959
protected $responseHeaders;
60-
60+
6161
/**
6262
* The deserialized response object
6363
* @var $responseObject;
6464
*/
6565
protected $responseObject;
66-
66+
6767
/**
6868
* Constructor
6969
* @param string $message Error message
70-
* @param string $code HTTP status code
70+
* @param int $code HTTP status code
7171
* @param string $responseHeaders HTTP response header
72-
* @param string $responseBody Deseralized response object
72+
* @param mixed $responseBody HTTP body of the server response either as Json or string
7373
*/
7474
public function __construct($message="", $code=0, $responseHeaders=null, $responseBody=null)
7575
{
7676
parent::__construct($message, $code);
7777
$this->responseHeaders = $responseHeaders;
7878
$this->responseBody = $responseBody;
7979
}
80-
80+
8181
/**
8282
* Gets the HTTP response header
8383
*
@@ -87,17 +87,17 @@ class ApiException extends Exception
8787
{
8888
return $this->responseHeaders;
8989
}
90-
90+
9191
/**
92-
* Gets the HTTP response body
92+
* Gets the HTTP body of the server response either as Json or string
9393
*
94-
* @return string HTTP response body
94+
* @return mixed HTTP body of the server response either as Json or string
9595
*/
9696
public function getResponseBody()
9797
{
9898
return $this->responseBody;
9999
}
100-
100+
101101
/**
102102
* Sets the deseralized response object (during deserialization)
103103
* @param mixed $obj Deserialized response object

0 commit comments

Comments
 (0)