Document the library error types

This commit is contained in:
Félix Saparelli 2022-05-31 21:15:52 +12:00
parent c0eaffb05d
commit f9e69503b0
No known key found for this signature in database
GPG key ID: B948C4BAE44FC474

View file

@ -2,35 +2,54 @@ use miette::Diagnostic;
use thiserror::Error; use thiserror::Error;
#[derive(Error, Diagnostic, Debug)] #[derive(Error, Diagnostic, Debug)]
#[diagnostic(url(docsrs))]
pub enum BinstallError { pub enum BinstallError {
/// The installation was cancelled by a user at a confirmation prompt.
#[error("installation cancelled by user")] #[error("installation cancelled by user")]
#[diagnostic(code(binstall::user_abort))] #[diagnostic(code(binstall::user_abort))]
UserAbort, UserAbort,
/// A generic I/O error.
#[error(transparent)] #[error(transparent)]
#[diagnostic(code(binstall::io))] #[diagnostic(code(binstall::io))]
Io(#[from] std::io::Error), Io(#[from] std::io::Error),
/// A URL is invalid.
///
/// This may be the result of a template in a Cargo manifest.
#[error(transparent)] #[error(transparent)]
#[diagnostic(code(binstall::url_parse))] #[diagnostic(code(binstall::url_parse))]
UrlParse(#[from] url::ParseError), UrlParse(#[from] url::ParseError),
/// A generic error from our HTTP client, reqwest.
///
/// Errors resulting from HTTP fetches are handled with [`BinstallError::Http`] instead.
#[error(transparent)] #[error(transparent)]
#[diagnostic(code(binstall::reqwest))] #[diagnostic(code(binstall::reqwest))]
Reqwest(#[from] reqwest::Error), Reqwest(#[from] reqwest::Error),
/// A rendering error in a template.
#[error(transparent)] #[error(transparent)]
#[diagnostic(code(binstall::template))] #[diagnostic(code(binstall::template))]
Template(#[from] tinytemplate::error::Error), Template(#[from] tinytemplate::error::Error),
/// An error while unzipping a file.
#[error(transparent)] #[error(transparent)]
#[diagnostic(code(binstall::unzip))] #[diagnostic(code(binstall::unzip))]
Unzip(#[from] zip::result::ZipError), Unzip(#[from] zip::result::ZipError),
/// A parsing or validation error in a cargo manifest.
///
/// This should be rare, as manifests are generally fetched from crates.io, which does its own
/// validation upstream. The most common failure will therefore be for direct repository access
/// and with the `--manifest-path` option.
#[error(transparent)] #[error(transparent)]
#[diagnostic(code(binstall::cargo_manifest))] #[diagnostic(code(binstall::cargo_manifest))]
CargoManifest(#[from] cargo_toml::Error), CargoManifest(#[from] cargo_toml::Error),
/// An error interacting with the crates.io API.
///
/// This could either be a "not found" or a server/transport error.
#[error("crates.io api error fetching crate information for '{crate_name}': {err}")] #[error("crates.io api error fetching crate information for '{crate_name}': {err}")]
#[diagnostic(code(binstall::crates_io_api))] #[diagnostic(code(binstall::crates_io_api))]
CratesIoApi { CratesIoApi {
@ -39,6 +58,10 @@ pub enum BinstallError {
err: crates_io_api::Error, err: crates_io_api::Error,
}, },
/// A version is not valid semver.
///
/// Note that we use the [`semver`] crate, which parses Cargo version syntax; this may be
/// somewhat stricter or very slightly different from other semver implementations.
#[error("version string '{v}' is not semver: {err}")] #[error("version string '{v}' is not semver: {err}")]
#[diagnostic(code(binstall::version::parse))] #[diagnostic(code(binstall::version::parse))]
VersionParse { VersionParse {
@ -47,6 +70,12 @@ pub enum BinstallError {
err: semver::Error, err: semver::Error,
}, },
/// A version requirement is not valid.
///
/// This is usually provided via the `--version` option.
///
/// Note that we use the [`semver`] crate, which parses Cargo version requirement syntax; they
/// may be slightly different from other semver requirements expressions implementations.
#[error("version requirement '{req}' is not semver: {err}")] #[error("version requirement '{req}' is not semver: {err}")]
#[diagnostic(code(binstall::version::requirement))] #[diagnostic(code(binstall::version::requirement))]
VersionReq { VersionReq {
@ -55,10 +84,17 @@ pub enum BinstallError {
err: semver::Error, err: semver::Error,
}, },
/// No available version matches the requirements.
///
/// This may be the case when using the `--version` option.
///
/// Note that using `--version 1.2.3` is interpreted as the requirement `^1.2.3` as per
/// Cargo.toml rules. If you want the exact version 1.2.3, use `--version '=1.2.3'`.
#[error("no version matching requirement '{req}'")] #[error("no version matching requirement '{req}'")]
#[diagnostic(code(binstall::version::mismatch))] #[diagnostic(code(binstall::version::mismatch))]
VersionMismatch { req: semver::VersionReq }, VersionMismatch { req: semver::VersionReq },
/// The crates.io API doesn't have manifest metadata for the given version.
#[error("no crate information available for '{crate_name}' version '{v}'")] #[error("no crate information available for '{crate_name}' version '{v}'")]
#[diagnostic(code(binstall::version::unavailable))] #[diagnostic(code(binstall::version::unavailable))]
VersionUnavailable { VersionUnavailable {
@ -66,6 +102,10 @@ pub enum BinstallError {
v: semver::Version, v: semver::Version,
}, },
/// An HTTP request failed.
///
/// This includes both connection/transport failures and when the HTTP status of the response
/// is not as expected.
#[error("could not {method} {url}: {err}")] #[error("could not {method} {url}: {err}")]
#[diagnostic(code(binstall::http))] #[diagnostic(code(binstall::http))]
Http { Http {