-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy patherror.rs
More file actions
181 lines (155 loc) · 5.01 KB
/
error.rs
File metadata and controls
181 lines (155 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::{env::JoinPathsError, ffi::OsStr, fmt::Display, path::Path, sync::Arc};
use vite_path::{AbsolutePath, relative::InvalidPathDataError};
use vite_str::Str;
use crate::{
context::{PlanContext, TaskCallStackDisplay, TaskRecursionError},
envs::ResolveEnvError,
};
#[derive(Debug, thiserror::Error)]
pub enum CdCommandError {
#[error("No home directory found for 'cd' command with no arguments")]
NoHomeDirectory,
#[error("Too many args for 'cd' command")]
ToManyArgs,
}
#[derive(Debug, thiserror::Error)]
pub struct WhichError {
pub program: Arc<OsStr>,
pub path_env: Option<Arc<OsStr>>,
pub cwd: Arc<AbsolutePath>,
#[source]
pub error: which::Error,
}
impl Display for WhichError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Failed to find executable {:?} under cwd {:?} with ", self.program, self.cwd)?;
if let Some(path_env) = &self.path_env {
write!(f, "PATH: {:?}", path_env)?
} else {
write!(f, "No PATH")?
}
Ok(())
}
}
#[derive(Debug, thiserror::Error)]
pub enum PathFingerprintErrorKind {
#[error("Path {path:?} is outside of the workspace {workspace_path:?}")]
PathOutsideWorkspace { path: Arc<AbsolutePath>, workspace_path: Arc<AbsolutePath> },
#[error("Path {path:?} contains characters that make it non-portable")]
NonPortableRelativePath {
path: Arc<Path>,
#[source]
error: InvalidPathDataError,
},
}
#[derive(Debug)]
pub enum PathType {
Cwd,
Program,
PackagePath,
}
impl Display for PathType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PathType::Cwd => write!(f, "current working directory"),
PathType::Program => write!(f, "program path"),
PathType::PackagePath => write!(f, "package path"),
}
}
}
#[derive(Debug, thiserror::Error)]
#[error("Failed to fingerprint {path_type}")]
pub struct PathFingerprintError {
pub path_type: PathType,
#[source]
pub kind: PathFingerprintErrorKind,
}
/// Errors that can occur when planning a specific execution from a task .
#[derive(Debug, thiserror::Error)]
pub enum TaskPlanErrorKind {
#[error("Failed to load task graph")]
TaskGraphLoadError(
#[source]
#[from]
vite_task_graph::TaskGraphLoadError,
),
#[error("Failed to execute 'cd' command")]
CdCommandError(
#[source]
#[from]
CdCommandError,
),
#[error(transparent)]
ProgramNotFound(#[from] WhichError),
#[error(transparent)]
PathFingerprintError(#[from] PathFingerprintError),
#[error("Failed to query tasks from task graph")]
TaskQueryError(
#[source]
#[from]
vite_task_graph::query::TaskQueryError,
),
#[error(transparent)]
TaskRecursionDetected(#[from] TaskRecursionError),
#[error("Invalid vite task command: {program} with args {args:?} under cwd {cwd:?}")]
ParsePlanRequestError {
program: Str,
args: Arc<[Str]>,
cwd: Arc<AbsolutePath>,
#[source]
error: anyhow::Error,
},
#[error("Failed to add node_modules/.bin to PATH environment variable")]
AddNodeModulesBinPathError {
#[source]
join_paths_error: JoinPathsError,
},
#[error("Failed to resolve environment variables")]
ResolveEnvError(#[source] ResolveEnvError),
}
#[derive(Debug, thiserror::Error)]
pub struct Error {
task_call_stack: TaskCallStackDisplay,
#[source]
kind: TaskPlanErrorKind,
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Failed to plan execution")?;
if !self.task_call_stack.is_empty() {
write!(f, ", task call stack: {}", self.task_call_stack)?
}
Ok(())
}
}
impl TaskPlanErrorKind {
pub fn with_empty_call_stack(self) -> Error {
Error { task_call_stack: TaskCallStackDisplay::default(), kind: self }
}
}
pub(crate) trait TaskPlanErrorKindResultExt {
type Ok;
/// Attach the current task call stack from the planning context to the error.
fn with_plan_context(self, context: &PlanContext<'_>) -> Result<Self::Ok, Error>;
/// Attach an empty task call stack to the error.
fn with_empty_call_stack(self) -> Result<Self::Ok, Error>;
}
impl<T> TaskPlanErrorKindResultExt for Result<T, TaskPlanErrorKind> {
type Ok = T;
/// Attach the current task call stack from the planning context to the error.
fn with_plan_context(self, context: &PlanContext<'_>) -> Result<T, Error> {
match self {
Ok(value) => Ok(value),
Err(kind) => {
let task_call_stack = context.display_call_stack();
Err(Error { task_call_stack, kind })
}
}
}
fn with_empty_call_stack(self) -> Result<T, Error> {
match self {
Ok(value) => Ok(value),
Err(kind) => Err(kind.with_empty_call_stack()),
}
}
}