Skip to main content

cloudflare_dns/api/
error.rs

1/// Custom error types for Cloudflare API operations.
2///
3/// This module provides domain-specific error types that enable better error handling
4/// and more user-friendly error messages compared to generic `anyhow::Error`.
5use thiserror::Error;
6
7/// Errors that can occur during Cloudflare API operations.
8#[derive(Debug, Error)]
9pub enum CloudflareError {
10    /// API returned an error response with specific error codes and messages.
11    #[allow(dead_code)]
12    #[error("API error: {message} (code: {code})")]
13    ApiError {
14        /// The error code from Cloudflare API
15        code: i64,
16        /// The error message from Cloudflare API
17        message: String,
18    },
19
20    /// Multiple errors returned from Cloudflare API.
21    #[error("API errors: {}", .0.join(", "))]
22    ApiErrors(Vec<String>),
23
24    /// HTTP request failed (network errors, timeouts, etc.).
25    #[error("HTTP request failed with status {status}: {body}")]
26    HttpError {
27        /// HTTP status code
28        status: u16,
29        /// Response body
30        body: String,
31    },
32
33    /// Failed to parse API response.
34    #[error("Failed to parse API response: {0}")]
35    ParseError(#[from] serde_json::Error),
36
37    /// Failed to send HTTP request.
38    #[error("Failed to send request: {0}")]
39    RequestError(#[from] reqwest::Error),
40
41    /// Record has no ID when trying to update.
42    #[error("Cannot update record: no ID provided")]
43    MissingRecordId,
44
45    /// No result returned from API.
46    #[error("No result returned from API")]
47    NoResult,
48}
49
50/// Result type alias for Cloudflare API operations.
51pub type CloudflareResult<T> = Result<T, CloudflareError>;