1use crate::config::{AnswerConfigError, CatalogError};
2use crate::system::SystemError;
3use crate::source::SourceError;
4use crate::ArchetypeError;
5use std::path::PathBuf;
6use std::fmt::{Display, Formatter};
7use std::error::Error;
8
9#[derive(Debug, thiserror::Error)]
10pub enum ArchetectError {
11 #[error("Error in answer file `{path}`: {source}")]
12 AnswerConfigError { path: String, source: AnswerConfigError },
13 #[error(transparent)]
14 ArchetypeError(#[from] ArchetypeError),
15 #[error(transparent)]
16 RenderError(#[from] RenderError),
17 #[error(transparent)]
18 SystemError(#[from] SystemError),
19 #[error(transparent)]
20 SourceError(#[from] SourceError),
21 #[error(transparent)]
22 CatalogError(#[from] CatalogError),
23 #[error(transparent)]
24 IoError(#[from] std::io::Error),
25 #[error("Headless mode requires answers to be supplied for all variables, but no answer was supplied for the `{0}` \
26 variable.")]
27 HeadlessMissingAnswer(String),
28 #[error("Headless mode attempted to use the default value for the `{identifier}` variable, however, {message}")]
29 HeadlessInvalidDefault { identifier: String, default: String, message: String },
30}
31
32#[derive(Debug, thiserror::Error)]
33pub enum RenderError {
34 InvalidPathCharacters {
35 path: PathBuf,
36 },
37 PathRenderError {
38 path: PathBuf,
39 source: crate::vendor::tera::Error,
40 },
41 FileRenderError {
42 path: PathBuf,
43 source: crate::vendor::tera::Error,
44 },
45 FileRenderIOError {
46 path: PathBuf,
47 source: std::io::Error,
48 },
49 StringRenderError {
50 string: String,
51 source: crate::vendor::tera::Error,
52 },
53 IOError {
54 #[from]
55 source: std::io::Error,
56 },
57}
58
59impl Display for RenderError {
60 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
61 match self {
62 RenderError::InvalidPathCharacters { path } => {
63 write!(f, "Invalid characters in path template `{:?}`", path)
64 }
65 RenderError::PathRenderError { path, source } => {
66 write!(f, "Unable to render path `{:?}`: {}", path, extract_tera_message(source))
67 }
68 RenderError::FileRenderError { path, source } => {
69 write!(f, "Unable to render file `{:?}`: {}", path, extract_tera_message(source))
70 }
71 RenderError::FileRenderIOError { path, source} => {
72 write!(f, "Unable to render file `{:?}`: {}", path, source)
73 }
74 RenderError::StringRenderError { string, source } => {
75 write!(f, "Unable to render string `{}`: {}", string, extract_tera_message(source))
76 }
77 RenderError::IOError { source } => {
78 write!(f, "Rendering IO Error: {}", source)
79 }
80 }
81 }
82}
83
84fn extract_tera_message(error: &crate::vendor::tera::Error) -> String {
85 match error.source() {
86 None => format!("{}", error),
87 Some(source) => format!("{}", source)
88 }
89}