Skip to content

Commit b16eda1

Browse files
committed
Improve error message for connection failures
Previous ApiException message would simply print out the result of the `curl_getinfo($curl)` call, which might be useful only if the developer actually wanted very low-level information from curl about why a call failed. The new message should print out a higher-level but more informative, human-readable message. If necessary for debugging, the ApiException's responseObject is set to the `curl_getinfo($curl)`.
1 parent 20448dd commit b16eda1

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,19 @@ class ApiClient
224224

225225
// Handle the response
226226
if ($response_info['http_code'] == 0) {
227-
throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null);
227+
$curl_error_message = curl_error($curl);
228+
229+
// curl_exec can sometimes fail but still return a blank message from curl_error().
230+
if (!empty($curl_error_message)) {
231+
$error_message = "API call to $url failed: $curl_error_message";
232+
} else {
233+
$error_message = "API call to $url failed, but for an unknown reason. " .
234+
"This could happen if you are disconnected from the network.";
235+
}
236+
237+
$exception = new ApiException($error_message, 0, null, null);
238+
$exception->setResponseObject($response_info);
239+
throw $exception;
228240
} elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299) {
229241
// return raw body if response is a file
230242
if ($responseType == '\SplFileObject' || $responseType == 'string') {

samples/client/petstore-security-test/php/SwaggerClient-php/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This spec is mainly for testing Petstore server and contains fake endpoints, mod
44
This PHP package is automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project:
55

66
- API version: 1.0.0 */ &#39; &quot; &#x3D;end
7-
- Build date: 2016-07-06T12:08:58.407-07:00
7+
- Build date: 2016-07-06T12:09:22.895-07:00
88
- Build package: class io.swagger.codegen.languages.PhpClientCodegen
99

1010
## Requirements

samples/client/petstore-security-test/php/SwaggerClient-php/lib/ApiClient.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,19 @@ public function callApi($resourcePath, $method, $queryParams, $postData, $header
245245

246246
// Handle the response
247247
if ($response_info['http_code'] == 0) {
248-
throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null);
248+
$curl_error_message = curl_error($curl);
249+
250+
// curl_exec can sometimes fail but still return a blank message from curl_error().
251+
if (!empty($curl_error_message)) {
252+
$error_message = "API call to $url failed: $curl_error_message";
253+
} else {
254+
$error_message = "API call to $url failed, but for an unknown reason. " .
255+
"This could happen if you are disconnected from the network.";
256+
}
257+
258+
$exception = new ApiException($error_message, 0, null, null);
259+
$exception->setResponseObject($response_info);
260+
throw $exception;
249261
} elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299) {
250262
// return raw body if response is a file
251263
if ($responseType == '\SplFileObject' || $responseType == 'string') {

samples/client/petstore/php/SwaggerClient-php/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This spec is mainly for testing Petstore server and contains fake endpoints, mod
44
This PHP package is automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project:
55

66
- API version: 1.0.0
7-
- Build date: 2016-07-06T12:01:52.156-07:00
7+
- Build date: 2016-07-06T12:05:01.729-07:00
88
- Build package: class io.swagger.codegen.languages.PhpClientCodegen
99

1010
## Requirements

samples/client/petstore/php/SwaggerClient-php/lib/ApiClient.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,19 @@ public function callApi($resourcePath, $method, $queryParams, $postData, $header
245245

246246
// Handle the response
247247
if ($response_info['http_code'] == 0) {
248-
throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null);
248+
$curl_error_message = curl_error($curl);
249+
250+
// curl_exec can sometimes fail but still return a blank message from curl_error().
251+
if (!empty($curl_error_message)) {
252+
$error_message = "API call to $url failed: $curl_error_message";
253+
} else {
254+
$error_message = "API call to $url failed, but for an unknown reason. " .
255+
"This could happen if you are disconnected from the network.";
256+
}
257+
258+
$exception = new ApiException($error_message, 0, null, null);
259+
$exception->setResponseObject($response_info);
260+
throw $exception;
249261
} elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299) {
250262
// return raw body if response is a file
251263
if ($responseType == '\SplFileObject' || $responseType == 'string') {

0 commit comments

Comments
 (0)