diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java
index d52b817990f..68aeb1666fa 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java
@@ -1026,6 +1026,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
supportingFiles.add(new SupportingFile("DateTimeJsonConverter.mustache", clientPackageDir, "DateTimeJsonConverter.cs"));
supportingFiles.add(new SupportingFile("DateTimeNullableJsonConverter.mustache", clientPackageDir, "DateTimeNullableJsonConverter.cs"));
supportingFiles.add(new SupportingFile("ApiResponseEventArgs`1.mustache", clientPackageDir, "ApiResponseEventArgs.cs"));
+ supportingFiles.add(new SupportingFile("ExceptionEventArgs.mustache", clientPackageDir, "ExceptionEventArgs.cs"));
supportingFiles.add(new SupportingFile("JsonSerializerOptionsProvider.mustache", clientPackageDir, "JsonSerializerOptionsProvider.cs"));
supportingFiles.add(new SupportingFile("CookieContainer.mustache", clientPackageDir, "CookieContainer.cs"));
supportingFiles.add(new SupportingFile("Option.mustache", clientPackageDir, "Option.cs"));
diff --git a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/ExceptionEventArgs.mustache b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/ExceptionEventArgs.mustache
new file mode 100644
index 00000000000..016ef7c697f
--- /dev/null
+++ b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/ExceptionEventArgs.mustache
@@ -0,0 +1,24 @@
+using System;
+
+namespace {{packageName}}.{{clientPackage}}
+{
+ ///
+ /// Useful for tracking server health
+ ///
+ {{>visibility}} class ExceptionEventArgs : EventArgs
+ {
+ ///
+ /// The ApiResponse
+ ///
+ public Exception Exception { get; }
+
+ ///
+ /// The ExcepetionEventArgs
+ ///
+ ///
+ public ExceptionEventArgs(Exception exception)
+ {
+ Exception = exception;
+ }
+ }
+}
diff --git a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/OnErrorDefaultImplementation.mustache b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/OnErrorDefaultImplementation.mustache
index bdefb67c6e4..7af8e07602d 100644
--- a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/OnErrorDefaultImplementation.mustache
+++ b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/OnErrorDefaultImplementation.mustache
@@ -1 +1,2 @@
- Logger.LogError(exception, "An error occurred while sending the request to the server.");
\ No newline at end of file
+ if (!suppressDefaultLog)
+ Logger.LogError(exception, "An error occurred while sending the request to the server.");
\ No newline at end of file
diff --git a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/api.mustache b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/api.mustache
index 75676b14a38..f101c1afe1b 100644
--- a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/api.mustache
+++ b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/api.mustache
@@ -81,11 +81,21 @@ namespace {{packageName}}.{{apiPackage}}
///
public event EventHandler>{{nrt?}} On{{operationId}};
+ ///
+ /// The event raised after an error querying the server
+ ///
+ public event EventHandler{{nrt?}} OnError{{operationId}};
+
internal void ExecuteOn{{operationId}}(ApiResponse<{{{returnType}}}{{^returnType}}object{{/returnType}}> apiResponse)
{
On{{operationId}}?.Invoke(this, new ApiResponseEventArgs<{{{returnType}}}{{^returnType}}object{{/returnType}}>(apiResponse));
}
+ internal void ExecuteOnError{{operationId}}(Exception exception)
+ {
+ OnError{{operationId}}?.Invoke(this, new ExceptionEventArgs(exception));
+ }
+
{{/operation}}
{{/lambda.trimTrailingWhiteSpace}}
}
@@ -232,20 +242,22 @@ namespace {{packageName}}.{{apiPackage}}
{{/allParams}}
private void OnError{{operationId}}DefaultImplementation({{#lambda.joinWithComma}}Exception exception string pathFormat string path {{#allParams}}{{^required}}Option<{{/required}}{{{dataType}}}{{>NullConditionalParameter}}{{^required}}>{{/required}} {{paramName}} {{/allParams}}{{/lambda.joinWithComma}})
{
+ bool suppressDefaultLog = false;
+ OnError{{operationId}}({{#lambda.joinWithComma}}ref suppressDefaultLog exception pathFormat path {{#allParams}}{{paramName}} {{/allParams}}{{/lambda.joinWithComma}});
{{>OnErrorDefaultImplementation}}
- OnError{{operationId}}({{#lambda.joinWithComma}}exception pathFormat path {{#allParams}}{{paramName}} {{/allParams}}{{/lambda.joinWithComma}});
}
///
/// A partial method that gives developers a way to provide customized exception handling
///
+ ///
///
///
///
{{#allParams}}
///
{{/allParams}}
- partial void OnError{{operationId}}({{#lambda.joinWithComma}}Exception exception string pathFormat string path {{#allParams}}{{^required}}Option<{{/required}}{{{dataType}}}{{>NullConditionalParameter}}{{^required}}>{{/required}} {{paramName}} {{/allParams}}{{/lambda.joinWithComma}});
+ partial void OnError{{operationId}}({{#lambda.joinWithComma}}ref bool suppressDefaultLog Exception exception string pathFormat string path {{#allParams}}{{^required}}Option<{{/required}}{{{dataType}}}{{>NullConditionalParameter}}{{^required}}>{{/required}} {{paramName}} {{/allParams}}{{/lambda.joinWithComma}});
///
/// {{summary}} {{notes}}
@@ -549,6 +561,7 @@ namespace {{packageName}}.{{apiPackage}}
catch(Exception e)
{
OnError{{operationId}}DefaultImplementation({{#lambda.joinWithComma}}e "{{path}}" uriBuilderLocalVar.Path {{#allParams}}{{paramName}} {{/allParams}}{{/lambda.joinWithComma}});
+ Events.ExecuteOnError{{operationId}}(e);
throw;
}
{{/lambda.trimLineBreaks}}
diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/.openapi-generator/FILES b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/.openapi-generator/FILES
index 60c779c5ced..04ee5f80fe4 100644
--- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/.openapi-generator/FILES
+++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/.openapi-generator/FILES
@@ -118,6 +118,7 @@ src/Org.OpenAPITools/Client/ClientUtils.cs
src/Org.OpenAPITools/Client/CookieContainer.cs
src/Org.OpenAPITools/Client/DateTimeJsonConverter.cs
src/Org.OpenAPITools/Client/DateTimeNullableJsonConverter.cs
+src/Org.OpenAPITools/Client/ExceptionEventArgs.cs
src/Org.OpenAPITools/Client/HostConfiguration.cs
src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs
src/Org.OpenAPITools/Client/HttpSigningToken.cs
diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/AnotherFakeApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/AnotherFakeApi.cs
index 81e2eb36092..695312bf4d1 100644
--- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/AnotherFakeApi.cs
+++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/AnotherFakeApi.cs
@@ -73,10 +73,20 @@ namespace Org.OpenAPITools.Api
///
public event EventHandler>? OnCall123TestSpecialTags;
+ ///
+ /// The event raised after an error querying the server
+ ///
+ public event EventHandler? OnErrorCall123TestSpecialTags;
+
internal void ExecuteOnCall123TestSpecialTags(ApiResponse apiResponse)
{
OnCall123TestSpecialTags?.Invoke(this, new ApiResponseEventArgs(apiResponse));
}
+
+ internal void ExecuteOnErrorCall123TestSpecialTags(Exception exception)
+ {
+ OnErrorCall123TestSpecialTags?.Invoke(this, new ExceptionEventArgs(exception));
+ }
}
///
@@ -191,18 +201,21 @@ namespace Org.OpenAPITools.Api
///
private void OnErrorCall123TestSpecialTagsDefaultImplementation(Exception exception, string pathFormat, string path, ModelClient modelClient)
{
- Logger.LogError(exception, "An error occurred while sending the request to the server.");
- OnErrorCall123TestSpecialTags(exception, pathFormat, path, modelClient);
+ bool suppressDefaultLog = false;
+ OnErrorCall123TestSpecialTags(ref suppressDefaultLog, exception, pathFormat, path, modelClient);
+ if (!suppressDefaultLog)
+ Logger.LogError(exception, "An error occurred while sending the request to the server.");
}
///
/// A partial method that gives developers a way to provide customized exception handling
///
+ ///
///
///
///
///
- partial void OnErrorCall123TestSpecialTags(Exception exception, string pathFormat, string path, ModelClient modelClient);
+ partial void OnErrorCall123TestSpecialTags(ref bool suppressDefaultLog, Exception exception, string pathFormat, string path, ModelClient modelClient);
///
/// To test special tags To test special tags and operation ID starting with number
@@ -291,6 +304,7 @@ namespace Org.OpenAPITools.Api
catch(Exception e)
{
OnErrorCall123TestSpecialTagsDefaultImplementation(e, "/another-fake/dummy", uriBuilderLocalVar.Path, modelClient);
+ Events.ExecuteOnErrorCall123TestSpecialTags(e);
throw;
}
}
diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/DefaultApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/DefaultApi.cs
index 14e463f368d..91831f2632e 100644
--- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/DefaultApi.cs
+++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/DefaultApi.cs
@@ -115,30 +115,60 @@ namespace Org.OpenAPITools.Api
///
public event EventHandler>? OnFooGet;
+ ///
+ /// The event raised after an error querying the server
+ ///
+ public event EventHandler? OnErrorFooGet;
+
internal void ExecuteOnFooGet(ApiResponse apiResponse)
{
OnFooGet?.Invoke(this, new ApiResponseEventArgs(apiResponse));
}
+ internal void ExecuteOnErrorFooGet(Exception exception)
+ {
+ OnErrorFooGet?.Invoke(this, new ExceptionEventArgs(exception));
+ }
+
///
/// The event raised after the server response
///
public event EventHandler>? OnGetCountry;
+ ///
+ /// The event raised after an error querying the server
+ ///
+ public event EventHandler? OnErrorGetCountry;
+
internal void ExecuteOnGetCountry(ApiResponse