Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": "^8.2",
"composer-runtime-api": "^2.0",
"guzzlehttp/guzzle": "^7.3",
"guzzlehttp/guzzle": "^7.3 || ^8.0",
"guzzlehttp/psr7": "^1.7 || ^2.0 || ^3.0"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion examples/EchoBot/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"slim/psr7": "^1.6",
"slim/http": "^1.3",
"monolog/monolog": "^3.0.0",
"guzzlehttp/guzzle": "^7.5",
"guzzlehttp/guzzle": "^8.0",
"guzzlehttp/psr7": "^3.0",
"php-di/slim-bridge": "^3.3",
"linecorp/line-bot-sdk": "*"
Expand Down
2 changes: 1 addition & 1 deletion examples/KitchenSink/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"slim/psr7": "^1.6",
"slim/http": "^1.3",
"monolog/monolog": "^3.0.0",
"guzzlehttp/guzzle": "^7.5",
"guzzlehttp/guzzle": "^8.0",
"guzzlehttp/psr7": "^3.0",
"php-di/slim-bridge": "^3.3",
"linecorp/line-bot-sdk": "*"
Expand Down
45 changes: 27 additions & 18 deletions generator/src/main/resources/line-bot-sdk-php-generator/api.pebble
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace {{ apiPackage }};

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Psr7\MultipartStream;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
Expand Down Expand Up @@ -229,14 +229,15 @@ class {{ operations.classname }}
$options = $this->createHttpClientOption();
try {
$response = $this->client->send($request, $options);
} catch (RequestException $e) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this catch structure changed: Guzzle 8 removed RequestException::getResponse(), which the previous code relied on. The response-carrying exceptions were moved to a new ResponseException subclass:

  Guzzle 7                                Guzzle 8
  ────────────────────────────────        ────────────────────────────────
  TransferException                       TransferException
  ├── RequestException                    ├── RequestException
  │    │  getResponse(): ?Response        │    │  (getResponse() removed!)
  │    └── BadResponseException           │    └── ResponseException  (new)
  │          4xx/5xx, response            │          │  getResponse(): Response
  │          guaranteed                   │          └── BadResponseException
  └── ConnectException                    └── NetworkException  (new)
                                               └── ConnectException

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want the equivalent exception, BadResponseException is not the right one. You should still catch RequestException and check if it's an instance of ResponseException to see if you can get the response headers or body.

} catch (BadResponseException $e) {
$response = $e->getResponse();
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
$e->getResponse() ? $e->getResponse()->getHeaders() : null,
$e->getResponse() ? (string) $e->getResponse()->getBody() : null
$response->getHeaders(),
(string) $response->getBody()
);
} catch (ConnectException $e) {
} catch (TransferException $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
Expand Down Expand Up @@ -453,17 +454,25 @@ class {{ operations.classname }}
{%- endif %}
},
function ($exception) {
$response = $exception->getResponse();
$statusCode = $response->getStatusCode();
throw new ApiException(
sprintf(
'[%d] Error connecting to the API (%s)',
if ($exception instanceof BadResponseException) {
$response = $exception->getResponse();
$statusCode = $response->getStatusCode();
throw new ApiException(
sprintf(
'[%d] Error connecting to the API (%s)',
$statusCode,
$exception->getRequest()->getUri()
),
$statusCode,
$exception->getRequest()->getUri()
),
$statusCode,
$response->getHeaders(),
(string) $response->getBody()
$response->getHeaders(),
(string) $response->getBody()
);
}
throw new ApiException(
"[{$exception->getCode()}] {$exception->getMessage()}",
(int) $exception->getCode(),
null,
null
);
}
);
Expand Down Expand Up @@ -621,7 +630,7 @@ class {{ operations.classname }}
if (isset(${{ p.paramName }})) {
if (stripos($headers['Content-Type'], 'application/json') !== false) {
# if Content-Type contains "application/json", json_encode the body
$httpBody = \GuzzleHttp\Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization(${{ p.paramName }}));
$httpBody = json_encode(ObjectSerializer::sanitizeForSerialization(${{ p.paramName }}), JSON_THROW_ON_ERROR);
} else {
$httpBody = ${{ p.paramName }};
}
Expand All @@ -646,7 +655,7 @@ class {{ operations.classname }}

} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
# if Content-Type contains "application/json", json_encode the form parameters
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
$httpBody = json_encode($formParams, JSON_THROW_ON_ERROR);
} else {
// for HTTP post (form)
$httpBody = ObjectSerializer::buildQuery($formParams);
Expand Down
2 changes: 1 addition & 1 deletion line-openapi
Loading