Skip to main content

storage_transfer_lib/
error.rs

1use std::fmt;
2
3use tokio::sync::Semaphore;
4
5#[derive(Debug)]
6pub enum Error {
7    InvalidLimit { field: &'static str, value: usize },
8    InvalidRoot,
9    InvalidConfig(&'static str),
10    UnsupportedEntry(String),
11    Storage(opendal::Error),
12    ConsumerClosed,
13    Unsupported(String),
14    SourceChanged(String),
15    Cancelled,
16    VerificationFailed(String),
17    Report(std::io::Error),
18    Task(tokio::task::JoinError),
19}
20
21pub type Result<T> = std::result::Result<T, Error>;
22
23impl fmt::Display for Error {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            Self::InvalidLimit { field, value } => {
27                write!(
28                    f,
29                    "{field} must be in 1..={}, got {value}",
30                    Semaphore::MAX_PERMITS
31                )
32            }
33            Self::InvalidConfig(field) => write!(f, "invalid transfer configuration: {field}"),
34            Self::InvalidRoot => write!(f, "operators must be rooted at the bucket root (/)"),
35            Self::UnsupportedEntry(path) => write!(f, "unknown entry type: {path}"),
36            Self::Storage(error) => write!(f, "storage operation failed: {error}"),
37            Self::Unsupported(reason) => write!(f, "unsupported copy: {reason}"),
38            Self::SourceChanged(path) => write!(
39                f,
40                "source object changed or returned an invalid range: {path}"
41            ),
42            Self::Cancelled => write!(f, "transfer cancelled"),
43            Self::VerificationFailed(path) => write!(f, "destination verification failed: {path}"),
44            Self::Report(error) => write!(f, "transfer report failed: {error}"),
45            Self::Task(error) => write!(f, "transfer task failed: {error}"),
46            Self::ConsumerClosed => {
47                write!(f, "object queue receiver closed before listing completed")
48            }
49        }
50    }
51}
52
53impl std::error::Error for Error {
54    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
55        match self {
56            Self::Storage(error) => Some(error),
57            Self::Task(error) => Some(error),
58            Self::Report(error) => Some(error),
59            _ => None,
60        }
61    }
62}
63
64impl From<opendal::Error> for Error {
65    fn from(error: opendal::Error) -> Self {
66        Self::Storage(error)
67    }
68}