Skip to main content

s3_filesystem/
error.rs

1use aws_sdk_s3::{error::SdkError, primitives::ByteStreamError};
2use std::{fmt::Debug, io};
3
4#[derive(Debug)]
5/// Container for errors that can occur due to AWS or local I/O.
6pub enum S3FilesystemError<E, R> {
7    /// Occurs when a request to S3 is unsuccessful - for instance when a non-existent object is requested.
8    S3(SdkError<E, R>),
9    /// Occurs when a reading or writing to/from a ByteStream (used for S3 downloads/uploads).
10    ByteStream(ByteStreamError),
11    /// Occurs when there are issues with the local file system - for instance, creating a file with an invalid character in the filename.
12    Io(io::Error),
13}
14
15impl<E, R> From<io::Error> for S3FilesystemError<E, R> {
16    fn from(err: io::Error) -> Self {
17        Self::Io(err)
18    }
19}
20
21impl<E, R> From<SdkError<E, R>> for S3FilesystemError<E, R> {
22    fn from(err: SdkError<E, R>) -> Self {
23        Self::S3(err)
24    }
25}
26
27impl<E, R> From<ByteStreamError> for S3FilesystemError<E, R> {
28    fn from(err: ByteStreamError) -> Self {
29        Self::ByteStream(err)
30    }
31}
32impl<E, R> std::fmt::Display for S3FilesystemError<E, R> {
33    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
34        match self {
35            S3FilesystemError::S3(s3_err) => write!(f, "S3 Error: {}", s3_err),
36            S3FilesystemError::Io(io_err) => write!(f, "IO Error: {}", io_err),
37            S3FilesystemError::ByteStream(bytestream_error) => {
38                write!(f, "ByteStream error: {}", bytestream_error)
39            }
40        }
41    }
42}
43impl<E, R> std::error::Error for S3FilesystemError<E, R>
44where
45    E: std::error::Error + 'static,
46    R: Debug,
47{
48}