dependency_check_updates_core/
error.rs1use miette::Diagnostic;
5use std::path::PathBuf;
6use thiserror::Error;
7
8#[derive(Debug, Error, Diagnostic)]
15#[non_exhaustive]
16pub enum DcuError {
17 #[error("failed to read manifest at {path}")]
19 #[diagnostic(
20 code(dependency_check_updates::io_error),
21 help("make sure the file exists and is readable")
22 )]
23 Io {
24 path: PathBuf,
26 #[source]
28 source: std::io::Error,
29 },
30
31 #[error("failed to parse manifest at {path}")]
33 #[diagnostic(code(dependency_check_updates::parse_error))]
34 ManifestParse {
35 path: PathBuf,
37 detail: String,
39 },
40
41 #[error("registry lookup failed for package `{package}`: {detail}")]
43 #[diagnostic(
44 code(dependency_check_updates::registry_error),
45 help("check your internet connection, or set GITHUB_TOKEN if scanning workflows")
46 )]
47 RegistryLookup {
48 package: String,
50 detail: String,
52 },
53
54 #[error("failed to apply patch to {path}")]
56 #[diagnostic(code(dependency_check_updates::patch_error))]
57 PatchFailed {
58 path: PathBuf,
60 detail: String,
62 },
63
64 #[error("invalid semver: {input}")]
66 #[diagnostic(code(dependency_check_updates::semver_error))]
67 SemverParse {
68 input: String,
70 detail: String,
72 },
73
74 #[error("no manifest found in {path}")]
76 #[diagnostic(
77 code(dependency_check_updates::no_manifest),
78 help(
79 "run dependency-check-updates in a directory containing package.json, or use --manifest"
80 )
81 )]
82 NoManifest {
83 path: PathBuf,
85 },
86}
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91 use rstest::rstest;
92
93 fn io_err() -> DcuError {
97 DcuError::Io {
98 path: PathBuf::from("test.json"),
99 source: std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"),
100 }
101 }
102
103 fn manifest_parse_err() -> DcuError {
104 DcuError::ManifestParse {
105 path: PathBuf::from("package.json"),
106 detail: "unexpected token".to_owned(),
107 }
108 }
109
110 fn no_manifest_err() -> DcuError {
111 DcuError::NoManifest {
112 path: PathBuf::from("/some/dir"),
113 }
114 }
115
116 fn registry_lookup_err() -> DcuError {
117 DcuError::RegistryLookup {
118 package: "lodash".to_owned(),
119 detail: "connection timeout".to_owned(),
120 }
121 }
122
123 fn semver_parse_err() -> DcuError {
124 DcuError::SemverParse {
125 input: "not.a.version".to_owned(),
126 detail: "invalid semver format".to_owned(),
127 }
128 }
129
130 #[rstest]
138 #[case::io(
139 io_err(),
140 format!("failed to read manifest at {}", PathBuf::from("test.json").display())
141 )]
142 #[case::manifest_parse(
143 manifest_parse_err(),
144 format!("failed to parse manifest at {}", PathBuf::from("package.json").display())
145 )]
146 #[case::no_manifest(
147 no_manifest_err(),
148 format!("no manifest found in {}", PathBuf::from("/some/dir").display())
149 )]
150 #[case::registry_lookup(
151 registry_lookup_err(),
152 "registry lookup failed for package `lodash`: connection timeout".to_owned()
153 )]
154 #[case::semver_parse(
155 semver_parse_err(),
156 "invalid semver: not.a.version".to_owned()
157 )]
158 fn dcu_error_display(#[case] err: DcuError, #[case] expected: String) {
159 assert_eq!(err.to_string(), expected);
160 }
161}