From 7ca0b92944438e908939518d476eeb2ead6822d9 Mon Sep 17 00:00:00 2001 From: TORISOUP Date: Wed, 8 Feb 2023 11:28:55 +0900 Subject: [PATCH] [csharp-netcore] HttpClient/Set TimeoutException to inner exception in case of timeout (#13862) * Changed to TimeoutException * Handle timeout exceptions as inner exceptions. * Corrected arguments for compatibility --- .../libraries/httpclient/ApiClient.mustache | 9 +++++++++ .../libraries/httpclient/ApiClient.mustache | 9 +++++++++ .../src/Org.OpenAPITools/Client/ApiClient.cs | 9 +++++++++ 3 files changed, 27 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore-functions/libraries/httpclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore-functions/libraries/httpclient/ApiClient.mustache index 2582692cc2..17e260a813 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore-functions/libraries/httpclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore-functions/libraries/httpclient/ApiClient.mustache @@ -531,6 +531,15 @@ namespace {{packageName}}.Client return await ToApiResponse(response, responseData, req.RequestUri); } + catch (OperationCanceledException original) + { + if (timeoutTokenSource != null && timeoutTokenSource.IsCancellationRequested) + { + throw new TaskCanceledException($"[{req.Method}] {req.RequestUri} was timeout.", + new TimeoutException(original.Message, original)); + } + throw; + } finally { if (timeoutTokenSource != null) diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache index 866f036e65..bbc95b2bd5 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache @@ -533,6 +533,15 @@ namespace {{packageName}}.Client return await ToApiResponse(response, responseData, req.RequestUri); } + catch (OperationCanceledException original) + { + if (timeoutTokenSource != null && timeoutTokenSource.IsCancellationRequested) + { + throw new TaskCanceledException($"[{req.Method}] {req.RequestUri} was timeout.", + new TimeoutException(original.Message, original)); + } + throw; + } finally { if (timeoutTokenSource != null) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/ApiClient.cs index 5bf010da66..2c37082b95 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/src/Org.OpenAPITools/Client/ApiClient.cs @@ -530,6 +530,15 @@ namespace Org.OpenAPITools.Client return await ToApiResponse(response, responseData, req.RequestUri); } + catch (OperationCanceledException original) + { + if (timeoutTokenSource != null && timeoutTokenSource.IsCancellationRequested) + { + throw new TaskCanceledException($"[{req.Method}] {req.RequestUri} was timeout.", + new TimeoutException(original.Message, original)); + } + throw; + } finally { if (timeoutTokenSource != null)