Path: redb-tsak/src/redb.Tsak.Client/ApiException.cs
Snapshot: redb-tsak, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-tsak
Page: https://redbase.app/src/dev-2026-12-06/redb-tsak/src/redb.Tsak.Client/ApiException.cs
----
using redb.Tsak.Contracts;
namespace redb.Tsak.Client;
///
/// Base exception thrown when the Tsak API returns a non-success status code.
/// Existing catch (ApiException) sites continue to work — typed subclasses below
/// (, , etc.) extend this type
/// for HTTP-status-driven control flow without breaking callers.
///
public class ApiException : Exception
{
/// HTTP status code returned by the API.
public int StatusCode { get; }
/// Parsed error response body, if available.
public ApiErrorResponse? ErrorResponse { get; }
///
/// Initializes a new API exception.
///
/// HTTP status code.
/// Error message.
/// Parsed error response, if available.
public ApiException(int statusCode, string message, ApiErrorResponse? errorResponse = null)
: base(message)
{
StatusCode = statusCode;
ErrorResponse = errorResponse;
}
///
/// Creates a typed subclass matching the HTTP status code.
/// Falls back to the base for status codes without a dedicated type.
///
public static ApiException Create(int statusCode, string message, ApiErrorResponse? errorResponse = null)
=> statusCode switch
{
400 => new BadRequestException(message, errorResponse),
401 => new UnauthorizedException(message, errorResponse),
403 => new ForbiddenException(message, errorResponse),
404 => new NotFoundException(message, errorResponse),
409 => new ConflictException(message, errorResponse),
429 => new ThrottledException(message, errorResponse),
_ => new ApiException(statusCode, message, errorResponse)
};
}
/// HTTP 400 Bad Request — request payload was rejected by the API.
public sealed class BadRequestException : ApiException
{
/// Initializes a new instance.
public BadRequestException(string message, ApiErrorResponse? errorResponse = null)
: base(400, message, errorResponse) { }
}
/// HTTP 401 Unauthorized — credentials missing or invalid.
public sealed class UnauthorizedException : ApiException
{
/// Initializes a new instance.
public UnauthorizedException(string message, ApiErrorResponse? errorResponse = null)
: base(401, message, errorResponse) { }
}
/// HTTP 403 Forbidden — caller authenticated but lacks permission.
public sealed class ForbiddenException : ApiException
{
/// Initializes a new instance.
public ForbiddenException(string message, ApiErrorResponse? errorResponse = null)
: base(403, message, errorResponse) { }
}
/// HTTP 404 Not Found — resource does not exist.
public sealed class NotFoundException : ApiException
{
/// Initializes a new instance.
public NotFoundException(string message, ApiErrorResponse? errorResponse = null)
: base(404, message, errorResponse) { }
}
/// HTTP 409 Conflict — request conflicts with current resource state.
public sealed class ConflictException : ApiException
{
/// Initializes a new instance.
public ConflictException(string message, ApiErrorResponse? errorResponse = null)
: base(409, message, errorResponse) { }
}
/// HTTP 429 Too Many Requests — caller exceeded a rate limit (e.g. AuthThrottle).
public sealed class ThrottledException : ApiException
{
/// Initializes a new instance.
public ThrottledException(string message, ApiErrorResponse? errorResponse = null)
: base(429, message, errorResponse) { }
}