devrc/
errors.rs

1use devrc_plugins::errors::DevrcPluginError;
2use reqwest::StatusCode;
3use serde_yaml::Error as SerdeYamlError;
4use std::{
5    error::Error as StdError,
6    fmt,
7    fmt::{Display, Formatter},
8    io::Error as IoError,
9    path::{PathBuf, StripPrefixError},
10};
11
12use tera::Error as TeraError;
13
14use dotenvy::{self, Error as DotenvError};
15
16use crate::resolver::Location;
17
18pub type DevrcResult<T> = Result<T, DevrcError>;
19
20#[derive(Debug)]
21pub enum DevrcError {
22    Dotenv(DotenvError),
23    NotExists,
24    FileNotExists(PathBuf),
25    PluginFileNotExists(PathBuf),
26    PluginError(DevrcPluginError),
27    GlobalNotExists,
28    LocalNotExists,
29    RenderError(TeraError),
30    EmptyVariable,
31    InvalidVariableType,
32    VariableTypeNotImplemented,
33    EmptyEnvironmentVariable,
34    IoError(IoError),
35    YamlParseError(SerdeYamlError),
36    TaskNotFound,
37    NotImplemented,
38    Signal,
39    Code {
40        code: i32,
41    },
42    CircularDependencies,
43    InvalidArgument,
44    InvalidName,
45    InvalidParams,
46    InvalidVariableName,
47    InvalidVariableModifier,
48    InvalidIncludeUrl(String),
49    TaskArgumentsParsingError,
50    OverlappingParameters,
51    NotEnouthArguments,
52    InvalidInterpreter,
53    NestingLevelExceed,
54    RuntimeError,
55    EnvfileImportError {
56        location: Location,
57    },
58    EnvfileUrlImportStatusError {
59        url: String,
60        status: StatusCode,
61    },
62    EnvfileUrlImportError {
63        url: String,
64        inner: reqwest::Error,
65    },
66    FileImportError,
67    UrlImportStatusError {
68        url: String,
69        status: StatusCode,
70    },
71    UrlImportRequestError {
72        url: String,
73        inner: reqwest::Error,
74    },
75    UrlImportError,
76    UrlImportHeadersError {
77        name: String,
78        value: String,
79    },
80    UrlImportChecksumError {
81        url: String,
82        control_checksum: String,
83        content_checksum: String,
84    },
85    AnyhowError(anyhow::Error),
86    HomeDirNotFound,
87    NetrcNotFound,
88    NetrcParsingError(netrc_rs::Error),
89}
90
91impl Display for DevrcError {
92    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
93        writeln!(f, "devrc error: ")?;
94
95        match self {
96            // TODO: add source context to error
97            DevrcError::RenderError(terra_error) => {
98                match TeraError::source(terra_error) {
99                    Some(value) => {
100                        write!(f, "{:}", &value)?;
101                    }
102                    _value => {
103                        writeln!(f, "another value")?;
104                    }
105                }
106                // write!(f, "{}: ", terra_error);
107            }
108            DevrcError::Code { code } => {
109                write!(f, "Recipe failed with code {:}", code)?;
110            }
111            DevrcError::FileNotExists(location) => {
112                write!(f, "File {:} not found", location.display())?;
113            }
114            DevrcError::InvalidIncludeUrl(url) => {
115                write!(f, "Invalid include url {:}", &url)?;
116            }
117            _ => {}
118        }
119        Ok(())
120    }
121}
122
123impl From<DotenvError> for DevrcError {
124    fn from(error: DotenvError) -> DevrcError {
125        DevrcError::Dotenv(error)
126    }
127}
128
129impl From<tera::Error> for DevrcError {
130    fn from(error: tera::Error) -> DevrcError {
131        DevrcError::RenderError(error)
132    }
133}
134
135impl From<IoError> for DevrcError {
136    fn from(error: IoError) -> DevrcError {
137        DevrcError::IoError(error)
138    }
139}
140
141impl StdError for DevrcError {}
142
143impl From<anyhow::Error> for DevrcError {
144    fn from(error: anyhow::Error) -> Self {
145        DevrcError::AnyhowError(error)
146    }
147}
148
149impl From<DevrcPluginError> for DevrcError {
150    fn from(value: DevrcPluginError) -> Self {
151        DevrcError::PluginError(value)
152    }
153}
154
155impl From<StripPrefixError> for DevrcError {
156    fn from(_: StripPrefixError) -> Self {
157        DevrcError::RuntimeError
158    }
159}