C# v4 SDKs now raise one family of exceptions. Everything the SDK throws for a call derives from SdkException, so a single catch covers every failure while the subclasses let you handle the ones you care about precisely.
Details
SdkException carries the failed call's Method and RequestUri, and every message starts with that call, so a log line identifies the failing request without a typed catch. The underlying cause is always the InnerException.
| Exception | When it is raised | Members it adds |
|---|---|---|
ApiException<TError> | The server answered with an error status | Error, plus StatusCode, Headers and ContentType from ApiException |
ResponseDeserializationException | A response body did not match the type the specification declares | TargetType, plus the ApiException members |
SdkConnectionException | The request could not be sent, or the response body could not be read | |
SdkTimeoutException | An attempt, the transport or an event stream went silent | Timeout |
AuthSchemeException | A credential could not be applied, such as an OAuth2 token request being refused | SchemeFailures |
TError is the operation's own error type when the specification declares error responses for it, with one TryGet accessor per declared error body and TryGetRawError for any other status. When the operation declares none, TError is RawError, which carries the status code and the raw body. Your own cancellation always surfaces as the usual OperationCanceledException and is never wrapped.
Usage Example
Catch from the most specific exception to the most general:
try
{
var payment = await client.Payments.CreatePayment(new CreatePaymentRequest { Body = newPayment });
}
catch (ApiException<CreatePaymentError> ex)
{
if (ex.Error.TryGetValidationError(out var validation))
Console.WriteLine($"Rejected ({(int)ex.StatusCode}): {validation.Message}");
else if (ex.Error.TryGetRawError(out var raw))
Console.WriteLine($"HTTP {(int)raw.StatusCode}: {raw.ReadAsString()}");
}
catch (SdkTimeoutException ex)
{
Console.WriteLine($"No answer within {ex.Timeout}");
}
catch (SdkConnectionException ex)
{
Console.WriteLine($"Could not reach the API: {ex.InnerException?.Message}");
}
catch (SdkException ex)
{
Console.WriteLine(ex.Message);
}
ResponseDeserializationException and AuthSchemeException both land in the final SdkException arm here. Give them their own arms when you need to tell them apart.
This feature is available in the C# v4 SDK (beta). Generate it from the APIMatic CLI:
apimatic sdk generate --language=csharp --codegen-version=v4 --stability=beta
For the full set of flags and options, see the APIMatic CLI documentation.