|
| 1 | +use std::{ffi::OsString, io, path::Path, sync::Arc}; |
| 2 | + |
| 3 | +use petgraph::algo::Cycle; |
| 4 | +use vite_path::{ |
| 5 | + AbsolutePath, RelativePathBuf, |
| 6 | + absolute::StripPrefixError, |
| 7 | + relative::{FromPathError, InvalidPathDataError}, |
| 8 | +}; |
| 9 | +use vite_str::Str; |
| 10 | + |
| 11 | +#[derive(Debug, thiserror::Error)] |
| 12 | +pub enum Error { |
| 13 | + // Task-specific errors (constructed in vite_task only) |
| 14 | + #[error("Duplicate package name `{name}` found at `{path1}` and `{path2}`")] |
| 15 | + DuplicatedPackageName { name: Str, path1: RelativePathBuf, path2: RelativePathBuf }, |
| 16 | + |
| 17 | + #[error("Package not found in workspace: `{0}`")] |
| 18 | + PackageNotFound(Str), |
| 19 | + |
| 20 | + #[error("Duplicate task: `{0}`")] |
| 21 | + DuplicatedTask(Str), |
| 22 | + |
| 23 | + #[error("Cycle dependencies detected: {0:?}")] |
| 24 | + CycleDependencies(Cycle<petgraph::graph::NodeIndex>), |
| 25 | + |
| 26 | + #[error("Task not found: `{task_request}`")] |
| 27 | + TaskNotFound { task_request: Str }, |
| 28 | + |
| 29 | + #[error("Task dependency `{name}` not found in package at `{package_path}`")] |
| 30 | + TaskDependencyNotFound { name: Str, package_path: RelativePathBuf }, |
| 31 | + |
| 32 | + #[error("Ambiguous task request: `{task_request}` (contains multiple '#')")] |
| 33 | + AmbiguousTaskRequest { task_request: Str }, |
| 34 | + |
| 35 | + #[error("Only one task is allowed in implicit mode (got: `{0}`)")] |
| 36 | + OnlyOneTaskRequest(Str), |
| 37 | + |
| 38 | + #[error("Recursive run with scoped task name is not supported: `{0}`")] |
| 39 | + RecursiveRunWithScope(Str), |
| 40 | + |
| 41 | + // Errors used by vite_task but not task-specific |
| 42 | + #[error("Unrecognized db version: {0}")] |
| 43 | + UnrecognizedDbVersion(u32), |
| 44 | + |
| 45 | + #[error("Env value is not valid unicode: {key} = {value:?}")] |
| 46 | + EnvValueIsNotValidUnicode { key: Str, value: OsString }, |
| 47 | + |
| 48 | + #[error( |
| 49 | + "The stripped path ({stripped_path:?}) is not a valid relative path because: {invalid_path_data_error}" |
| 50 | + )] |
| 51 | + StripPath { stripped_path: Box<Path>, invalid_path_data_error: InvalidPathDataError }, |
| 52 | + |
| 53 | + #[error("The path ({path:?}) is not a valid relative path because: {reason}")] |
| 54 | + InvalidRelativePath { path: Box<Path>, reason: FromPathError }, |
| 55 | + |
| 56 | + #[error("IO error: {err} at {path:?}")] |
| 57 | + IoWithPath { err: io::Error, path: Arc<AbsolutePath> }, |
| 58 | + |
| 59 | + #[cfg(unix)] |
| 60 | + #[error("Unsupported file type: {0:?}")] |
| 61 | + UnsupportedFileType(nix::dir::Type), |
| 62 | + |
| 63 | + #[cfg(windows)] |
| 64 | + #[error("Unsupported file type: {0:?}")] |
| 65 | + UnsupportedFileType(std::fs::FileType), |
| 66 | + |
| 67 | + // External library errors |
| 68 | + #[error(transparent)] |
| 69 | + Io(#[from] io::Error), |
| 70 | + |
| 71 | + #[error(transparent)] |
| 72 | + JoinPathsError(#[from] std::env::JoinPathsError), |
| 73 | + |
| 74 | + #[error(transparent)] |
| 75 | + WaxBuild(#[from] wax::BuildError), |
| 76 | + |
| 77 | + #[error(transparent)] |
| 78 | + WaxWalk(#[from] wax::WalkError), |
| 79 | + |
| 80 | + #[error(transparent)] |
| 81 | + Utf8Error(#[from] bstr::Utf8Error), |
| 82 | + |
| 83 | + #[error(transparent)] |
| 84 | + Serde(#[from] serde_json::Error), |
| 85 | + |
| 86 | + #[error(transparent)] |
| 87 | + Sqlite(#[from] rusqlite::Error), |
| 88 | + |
| 89 | + #[error(transparent)] |
| 90 | + BincodeEncode(#[from] bincode::error::EncodeError), |
| 91 | + |
| 92 | + #[error(transparent)] |
| 93 | + BincodeDecode(#[from] bincode::error::DecodeError), |
| 94 | + |
| 95 | + #[error(transparent)] |
| 96 | + Anyhow(#[from] anyhow::Error), |
| 97 | + |
| 98 | + #[error(transparent)] |
| 99 | + Glob(#[from] vite_glob::Error), |
| 100 | + |
| 101 | + #[error(transparent)] |
| 102 | + Workspace(#[from] vite_workspace::Error), |
| 103 | + |
| 104 | + #[cfg(unix)] |
| 105 | + #[error(transparent)] |
| 106 | + Nix(#[from] nix::Error), |
| 107 | +} |
| 108 | + |
| 109 | +impl From<StripPrefixError<'_>> for Error { |
| 110 | + fn from(value: StripPrefixError<'_>) -> Self { |
| 111 | + Self::StripPath { |
| 112 | + stripped_path: Box::from(value.stripped_path), |
| 113 | + invalid_path_data_error: value.invalid_path_data_error, |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments