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
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,17 @@ namespace {{packageName}}.Client
}
catch (Exception ex)
{
{{#throwOnAnyError}}
// 4xx/5xx are left to ExceptionFactory, like the ErrorException check above
if ((int)response.StatusCode < 400)
{
var error = ex.InnerException != null ? ex.InnerException : ex;
throw new ApiException((int)response.StatusCode, string.Format("Error calling {0}: {1}", request.Resource, error.Message), response.Content, ToApiResponse(response).Headers, error);
}
{{/throwOnAnyError}}
{{^throwOnAnyError}}
throw ex.InnerException != null ? ex.InnerException : ex;
{{/throwOnAnyError}}
}
}
else if (typeof(T).Name == "Stream") // for binary response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,17 @@ namespace {{packageName}}.Client
}
catch (Exception ex)
{
{{#throwOnAnyError}}
// 4xx/5xx are left to ExceptionFactory, like the ErrorException check above
if ((int)response.StatusCode < 400)
{
var error = ex.InnerException != null ? ex.InnerException : ex;
throw new ApiException((int)response.StatusCode, string.Format("Error calling {0}: {1}", request.Resource, error.Message), response.Content, ToApiResponse(response).Headers, error);
}
{{/throwOnAnyError}}
{{^throwOnAnyError}}
throw ex.InnerException != null ? ex.InnerException : ex;
{{/throwOnAnyError}}
}
}
else if (typeof(T).Name == "Stream") // for binary response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,46 @@ public void testUserAgentIsNotUrlEncoded() throws IOException {
}
}

@Test
public void testThrowOnAnyErrorWrapsOneOfDeserializationFailure() throws IOException {
// both restsharp ApiClient templates: the default one and the useIntForTimeout v7.9.0 fallback
for (boolean useIntForTimeout : new boolean[]{false, true}) {
for (boolean throwOnAnyError : new boolean[]{false, true}) {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore.yaml");
final DefaultGenerator defaultGenerator = new DefaultGenerator();
final ClientOptInput clientOptInput = new ClientOptInput();
clientOptInput.openAPI(openAPI);
CSharpClientCodegen cSharpClientCodegen = new CSharpClientCodegen();
cSharpClientCodegen.setLibrary("restsharp");
cSharpClientCodegen.setOutputDir(output.getAbsolutePath());
cSharpClientCodegen.additionalProperties().put("useIntForTimeout", useIntForTimeout);
cSharpClientCodegen.additionalProperties().put("throwOnAnyError", throwOnAnyError);
clientOptInput.config(cSharpClientCodegen);
defaultGenerator.opts(clientOptInput);

Map<String, File> files = defaultGenerator.generate().stream()
.collect(Collectors.toMap(File::getPath, Function.identity()));

File apiClient = files
.get(Paths.get(output.getAbsolutePath(), "src", "Org.OpenAPITools", "Client", "ApiClient.cs").toString());
assertNotNull(apiClient);
String wrapped = "throw new ApiException((int)response.StatusCode, string.Format(\"Error calling {0}: {1}\", request.Resource, error.Message), response.Content, ToApiResponse(response).Headers, error);";
// 4xx/5xx must still reach ExceptionFactory, so only lower status codes are wrapped here
String wrappedBelow400 = "if ((int)response.StatusCode < 400) { var error = ex.InnerException != null ? ex.InnerException : ex; " + wrapped + " }";
String rethrown = "throw ex.InnerException != null ? ex.InnerException : ex;";
if (throwOnAnyError) {
assertFileContains(apiClient.toPath(), wrappedBelow400);
assertFileNotContains(apiClient.toPath(), rethrown);
} else {
assertFileContains(apiClient.toPath(), rethrown);
assertFileNotContains(apiClient.toPath(), wrapped);
}
}
}
}

@Test
public void testSelectHeaderAcceptKeepsAllTypesJsonFirst() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,12 @@ private async Task<ApiResponse<T>> ExecClientAsync<T>(Func<RestClient, Task<Rest
}
catch (Exception ex)
{
throw ex.InnerException != null ? ex.InnerException : ex;
// 4xx/5xx are left to ExceptionFactory, like the ErrorException check above
if ((int)response.StatusCode < 400)
{
var error = ex.InnerException != null ? ex.InnerException : ex;
throw new ApiException((int)response.StatusCode, string.Format("Error calling {0}: {1}", request.Resource, error.Message), response.Content, ToApiResponse(response).Headers, error);
}
}
}
else if (typeof(T).Name == "Stream") // for binary response
Expand Down
Loading