Skip to main content

Exception

These CodeGen configurations allow you to control exceptions thrown in APIMatic generated SDKs.

Error Templates

This setting allows the configuration of error messages using custom templates that can be provided for overriding default exception messages thrown in SDKs for error responses.

Usage

To use this feature, you need to specify a Map<string, string> value, where keys would be error codes or ranges and values would be template error messages. By default, its value is set to null.

"info": {
...
"x-codegen-settings": {
"ErrorTemplates": {
"401": "Failed to authorize, Code: {$statusCode}.",
"5XX": "Internal server error, Code: {$statusCode}.",
"0": "An error occurred. Code: {$statusCode}"
}
}
}

Language Support

C#JavaPHPPythonRubyTSGo
✔️✔️✔️✔️✔️✔️✔️

Change in SDK

Configuring this setting will affect the error messages thrown on error codes for all SDKs. This behavior is documented in detail here

Resolve Exception Property Collisions in CSharp

This setting controls how the .NET SDK generator handles naming conflicts in generated exception models that overlap with System.Exception members.

When enabled, the generator resolves conflicts by post fixing properties instead of overriding base exception members.

Conflicting properties include: Message, ResponseCode, StackTrace, Source, Data, HelpLink, HttpContext, InnerException, HResult, TargetSite.

Impact

  • Conflicting exception property names may be changed (for example: MessageMessageProperty).
  • Existing code that accesses the original property names may need to be updated.
  • Prevents hiding base exception members and eliminates related compiler warnings.

Usage

To use this feature, you need to specify a Boolean value. By default, its value is set to false.

"info": {
...,
"x-codegen-settings": {
"CSharpResolveExceptionPropertyCollisions": true
}
}

Language Support

This setting only applies to .NET SDKs.

Change in SDK

Configuring this setting has the following effect on the generated SDK:

Value Change
true
/// <summary>
/// Error.
/// </summary>
public class Error : ApiException
{
/// <summary>
/// The human-readable, unique name of the error.
/// </summary>
[JsonProperty("name")]
public string Name { get; set; }

/// <summary>
/// The message that describes the error.
/// </summary>
[JsonProperty("message")]
public string MessageProperty { get; set; }

...
}
false (default)
/// <summary>
/// Error.
/// </summary>
public class Error : ApiException
{
/// <summary>
/// The human-readable, unique name of the error.
/// </summary>
[JsonProperty("name")]
public string Name { get; set; }

/// <summary>
/// The message that describes the error.
/// </summary>
[JsonProperty("message")]
public new string Message { get; set; }

...
}