aws_runtime/env_config/
error.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Errors related to AWS profile config files
7
8use crate::env_config::parse::EnvConfigParseError;
9use std::error::Error;
10use std::fmt::{Display, Formatter};
11use std::path::PathBuf;
12use std::sync::Arc;
13
14/// Failed to read or parse the profile file(s)
15#[derive(Debug, Clone)]
16pub enum EnvConfigFileLoadError {
17    /// The profile could not be parsed
18    #[non_exhaustive]
19    ParseError(EnvConfigParseError),
20
21    /// Attempt to read the AWS config file (`~/.aws/config` by default) failed with a filesystem error.
22    #[non_exhaustive]
23    CouldNotReadFile(CouldNotReadConfigFile),
24}
25
26impl Display for EnvConfigFileLoadError {
27    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28        match self {
29            EnvConfigFileLoadError::ParseError(_err) => {
30                write!(f, "could not parse profile file")
31            }
32            EnvConfigFileLoadError::CouldNotReadFile(err) => {
33                write!(f, "could not read file `{}`", err.path.display())
34            }
35        }
36    }
37}
38
39impl Error for EnvConfigFileLoadError {
40    fn source(&self) -> Option<&(dyn Error + 'static)> {
41        match self {
42            EnvConfigFileLoadError::ParseError(err) => Some(err),
43            EnvConfigFileLoadError::CouldNotReadFile(details) => Some(&details.cause),
44        }
45    }
46}
47
48impl From<EnvConfigParseError> for EnvConfigFileLoadError {
49    fn from(err: EnvConfigParseError) -> Self {
50        EnvConfigFileLoadError::ParseError(err)
51    }
52}
53
54/// An error encountered while reading the AWS config file
55#[derive(Debug, Clone)]
56pub struct CouldNotReadConfigFile {
57    pub(crate) path: PathBuf,
58    pub(crate) cause: Arc<std::io::Error>,
59}