axoproject/
errors.rs

1//! Errors!
2
3use std::string::FromUtf8Error;
4
5use camino::Utf8PathBuf;
6use miette::Diagnostic;
7use thiserror::Error;
8
9use crate::Version;
10
11/// A Result returned by Axoproject
12pub type Result<T> = std::result::Result<T, AxoprojectError>;
13
14/// An Error/Diagnostic returned by Axoproject
15#[derive(Debug, Error, Diagnostic)]
16#[non_exhaustive]
17pub enum AxoprojectError {
18    /// Axoasset returned an error (I/O error)
19    #[error(transparent)]
20    #[diagnostic(transparent)]
21    Axoasset(#[from] axoasset::AxoassetError),
22
23    /// Axoprocess returned an error (I/O error)
24    #[error(transparent)]
25    #[diagnostic(transparent)]
26    Axoprocess(#[from] axoprocess::AxoprocessError),
27
28    /// An error occurred in guppy/cargo-metadata when trying to find a cargo project
29    #[cfg(feature = "cargo-projects")]
30    #[error(transparent)]
31    CargoMetadata(#[from] guppy::Error),
32
33    /// An error occurred in parse_changelog
34    #[error(transparent)]
35    ParseChangelog(#[from] parse_changelog::Error),
36
37    /// An error parsing a dist manifest
38    #[error(transparent)]
39    #[diagnostic(transparent)]
40    Generic(#[from] GenericManifestParseError),
41
42    /// An error producing a string
43    #[error(transparent)]
44    FromUtf8(#[from] FromUtf8Error),
45
46    /// An error parsing a Cargo.toml
47    #[cfg(feature = "cargo-projects")]
48    #[error("couldn't read Cargo.toml")]
49    ParseCargoToml {
50        /// The toml file
51        #[source_code]
52        source: axoasset::SourceFile,
53        /// Where we found an issue
54        #[label]
55        span: Option<miette::SourceSpan>,
56        /// The underlying issue
57        #[source]
58        details: axoasset::toml_edit::TomlError,
59    },
60
61    /// We found a package.json but it didn't have "name" set
62    #[cfg(feature = "npm-projects")]
63    #[error("your package doesn't have a name:\n{manifest}")]
64    #[diagnostic(help("is it a workspace? We don't support that yet."))]
65    NamelessNpmPackage {
66        /// path to the package.json
67        manifest: Utf8PathBuf,
68    },
69
70    /// We tried to get the bins from a package.json but something went wrong
71    #[cfg(feature = "npm-projects")]
72    #[error("Failed to read the binaries from your package.json:\n{manifest_path}")]
73    BuildInfoParse {
74        /// Path to the package.json
75        manifest_path: Utf8PathBuf,
76        /// underlying error
77        #[source]
78        details: std::io::Error,
79    },
80
81    /// Your workspace gave several different values for "repository"
82    #[error("your workspace has inconsistent values for 'repository', refusing to select one:\n  {file1}:\n    {url1}\n  {file2}:\n    {url2}")]
83    #[diagnostic(severity("warning"))]
84    InconsistentRepositoryKey {
85        /// Path to the first manifest
86        file1: Utf8PathBuf,
87        /// value the first manifest had set
88        url1: String,
89        /// Path to the second manifest
90        file2: Utf8PathBuf,
91        /// value the second manifest had set
92        url2: String,
93    },
94
95    /// An error that occurred while trying to find READMEs and whatnot in your project dir
96    #[error("couldn't search for files in\n{dir}")]
97    AutoIncludeSearch {
98        /// path to the dir we were searching
99        dir: Utf8PathBuf,
100        /// underlying error
101        #[source]
102        details: std::io::Error,
103    },
104
105    /// An error that occurred while trying to parse a repository string
106    #[error("Your repository URL {url} couldn't be parsed.")]
107    #[diagnostic(help("only git-compatible URLs are supported."))]
108    UnknownRepoStyle {
109        /// URL to the repository
110        url: String,
111    },
112
113    /// An error that occurred because a repository string could not be parsed for a specific reason
114    #[error("failed to parse your repo, current config has repo as: {repo}")]
115    #[diagnostic(help("We found a repo url but we had trouble parsing it. Please make sure it's entered correctly. This may be an error, and if so you should file an issue."))]
116    RepoParseError {
117        /// URL to the repository
118        repo: String,
119    },
120
121    /// An error that occurred when parsing a repository string
122    #[error(transparent)]
123    UrlParseError(#[from] url::ParseError),
124
125    /// An error returned when a non-GitHub URL is parsed
126    #[error("Your repository URL {url} couldn't be parsed.")]
127    #[diagnostic(help("Only GitHub URLs are supported at the moment."))]
128    NotGitHubError {
129        /// URL to the repository
130        url: String,
131    },
132
133    /// We searched a changelog file but found no result
134    #[error("couldn't find a suitable changelog entry for {version} in {path}")]
135    ChangelogVersionNotFound {
136        /// Path of the file
137        path: Utf8PathBuf,
138        /// Version we were looking for
139        version: Version,
140    },
141
142    /// We couldn't parse a workspace because there's no Cargo
143    #[error("Your app has a Cargo.toml, but you don't appear to have cargo installed.")]
144    #[diagnostic(help("Is cargo in your PATH? You can install cargo via: https://rustup.rs"))]
145    CargoMissing {},
146}
147
148/// Errors related to finding the project
149#[derive(Debug, Error, Diagnostic)]
150pub enum ProjectError {
151    /// No workspace found from axoproject
152    #[error("No workspace found; either your project doesn't have a Cargo.toml/dist.toml, or we couldn't read it")]
153    ProjectMissing {
154        /// axoproject's error for the unidentified project
155        #[related]
156        sources: Vec<AxoprojectError>,
157    },
158
159    /// Found a workspace but it was malformed
160    #[error("We encountered an issue trying to read your workspace")]
161    ProjectBroken {
162        /// The cause
163        #[diagnostic_source]
164        cause: AxoprojectError,
165    },
166}
167
168/// Errors parsing a dist.toml or dist-workspaces.toml
169#[derive(Debug, Error, Diagnostic)]
170pub enum GenericManifestParseError {
171    /// No prefix in member
172    #[error(
173        r#"dist workspace member {val} is missing prefix
174members should be formatted like "dist:some/path
175possible prefixes are: dist, cargo, npm"#
176    )]
177    NoPrefix {
178        /// Raw entry in the list of members
179        val: String,
180    },
181    /// Unknown prefix in member
182    #[error(
183        "dist workspace member {val} has unknown {prefix} prefix
184possible prefixes are: dist, cargo, npm"
185    )]
186    UnknownPrefix {
187        /// prefix parsed out
188        prefix: String,
189        /// Raw entry in the list of members
190        val: String,
191    },
192}