Skip to main content

Module http_error

Module http_error 

Source
Expand description

HTTP client error types with comprehensive retry detection.

This module provides error types for HTTP operations with built-in support for retry detection, error categorization, and detailed error information.

§Overview

The HttpError enum covers all possible failure modes for HTTP requests:

  • Network errors (connection failures, DNS issues)
  • HTTP errors (4xx client errors, 5xx server errors)
  • Serialization/deserialization errors
  • Authentication errors
  • Timeouts
  • Configuration errors

§Retry Detection

The error type includes built-in retry detection via HttpError::is_retryable():

  • Network errors → retryable
  • Timeouts → retryable
  • 429 (rate limit) → retryable
  • 500, 502, 503, 504 (server errors) → retryable
  • All other errors → not retryable

When using the generated HTTP client with retry middleware (reqwest-retry), retryable errors are automatically retried with exponential backoff.

§Examples

§Basic Error Handling

fn handle_api_error(error: HttpError) {
    match error {
        HttpError::Network(e) => {
            eprintln!("Network error: {}", e);
            // Will be retried automatically if retry is configured
        }
        HttpError::Http { status, message, .. } => {
            match status {
                400 => eprintln!("Bad request: {}", message),
                401 => eprintln!("Unauthorized - check API key"),
                404 => eprintln!("Not found"),
                429 => eprintln!("Rate limited - will retry"),
                500..=599 => eprintln!("Server error: {}", message),
                _ => eprintln!("HTTP error {}: {}", status, message),
            }
        }
        HttpError::Timeout => {
            eprintln!("Request timeout - will retry");
        }
        e => {
            eprintln!("Other error: {}", e);
        }
    }
}

§Retry Detection

fn classify_error(error: &HttpError) {
    if error.is_retryable() {
        println!("Retryable error: {}", error);
        // If retry middleware is configured, this will be retried automatically
    } else if error.is_client_error() {
        println!("Client error (4xx): fix the request");
    } else if error.is_server_error() {
        println!("Server error (5xx): may be transient");
    } else {
        println!("Non-retryable error: {}", error);
    }
}

§Creating Errors

use openapi_to_rust::http_error::HttpError;

// Create HTTP error from status code
let error = HttpError::from_status(404, "Resource not found", None);

// Create serialization error
let error = HttpError::serialization_error("invalid JSON");

// Create deserialization error
let error = HttpError::deserialization_error("unexpected field");

§Integration with reqwest-retry

When the generated HTTP client is configured with retry middleware, the retry logic automatically handles retryable errors:

[http_client.retry]
max_retries = 3
initial_delay_ms = 500
max_delay_ms = 16000

The retry middleware uses exponential backoff and will retry:

  • Network errors (connection failures)
  • Timeouts
  • HTTP 429 (rate limit)
  • HTTP 500, 502, 503, 504 (server errors)

§Error Categories

Errors can be categorized using helper methods:

Structs§

ApiError
Envelope for an API response we received but couldn’t (or didn’t) treat as success.

Enums§

ApiOpError
Result error type for generated operation methods.
HttpError
HTTP client errors that can occur during API requests

Type Aliases§

HttpResult
Result type for HTTP operations