Skip to main content

Crate recoverable

Crate recoverable 

Source
Expand description

Recovery classifications for operations.

This crate reports whether a recovery attempt can change an outcome. Error types and resilience middleware can share this information. Use RecoveryInfo::never for successful operations and permanent failures.

§Core types

  • RecoveryInfo contains a recovery classification and an optional delay.
  • Recovery lets a type provide its recovery information.
  • RecoveryKind identifies the recovery classification.

§Examples

§Classify errors

use recoverable::{Recovery, RecoveryInfo, RecoveryKind};

#[derive(Debug)]
enum DatabaseError {
    ConnectionTimeout,
    InvalidCredentials,
    TableNotFound,
}

impl Recovery for DatabaseError {
    fn recovery(&self) -> RecoveryInfo {
        match self {
            // A retry can recover from this transient failure.
            DatabaseError::ConnectionTimeout => RecoveryInfo::retry(),
            // A retry cannot recover from these permanent failures.
            DatabaseError::InvalidCredentials => RecoveryInfo::never(),
            DatabaseError::TableNotFound => RecoveryInfo::never(),
        }
    }
}

let error = DatabaseError::ConnectionTimeout;
assert_eq!(error.recovery().kind(), RecoveryKind::Retry);

// Successful operations also use never() because they need no recovery.
let success_result: Result<(), DatabaseError> = Ok(());

§Set a retry delay

Use RecoveryInfo::delay to specify when to retry an operation.

use std::time::Duration;

use recoverable::{RecoveryInfo, RecoveryKind};

// Retry after 30 seconds, as specified by a Retry-After header.
let recovery = RecoveryInfo::retry().delay(Duration::from_secs(30));
assert_eq!(recovery.kind(), RecoveryKind::Retry);
assert_eq!(recovery.get_delay(), Some(Duration::from_secs(30)));

// Retry immediately.
let immediate = RecoveryInfo::retry().delay(Duration::ZERO);
assert_eq!(immediate.get_delay(), Some(Duration::ZERO));

Modules§

documentation
Detailed guides for recoverable.

Structs§

RecoveryInfo
Contains the recovery classification and optional delay for an operation or condition.

Enums§

RecoveryKind
Classifies recovery for an operation or condition.

Traits§

Recovery
Provides recovery information for a value.