Skip to content

Commit 944bf32

Browse files
committed
refactor(gctx): extract error to a module
1 parent 81c3f77 commit 944bf32

File tree

2 files changed

+111
-99
lines changed

2 files changed

+111
-99
lines changed

src/cargo/util/context/error.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
use std::fmt;
2+
3+
use crate::util::ConfigValue;
4+
use crate::util::context::ConfigKey;
5+
use crate::util::context::Definition;
6+
7+
/// Internal error for serde errors.
8+
#[derive(Debug)]
9+
pub struct ConfigError {
10+
error: anyhow::Error,
11+
definition: Option<Definition>,
12+
}
13+
14+
impl ConfigError {
15+
pub(super) fn new(message: String, definition: Definition) -> ConfigError {
16+
ConfigError {
17+
error: anyhow::Error::msg(message),
18+
definition: Some(definition),
19+
}
20+
}
21+
22+
pub(super) fn expected(key: &ConfigKey, expected: &str, found: &ConfigValue) -> ConfigError {
23+
ConfigError {
24+
error: anyhow::anyhow!(
25+
"`{}` expected {}, but found a {}",
26+
key,
27+
expected,
28+
found.desc()
29+
),
30+
definition: Some(found.definition().clone()),
31+
}
32+
}
33+
34+
pub(super) fn is_missing_field(&self) -> bool {
35+
self.error.downcast_ref::<MissingFieldError>().is_some()
36+
}
37+
38+
pub(super) fn missing(key: &ConfigKey) -> ConfigError {
39+
ConfigError {
40+
error: anyhow::anyhow!("missing config key `{}`", key),
41+
definition: None,
42+
}
43+
}
44+
45+
pub(super) fn with_key_context(
46+
self,
47+
key: &ConfigKey,
48+
definition: Option<Definition>,
49+
) -> ConfigError {
50+
ConfigError {
51+
error: anyhow::Error::from(self)
52+
.context(format!("could not load config key `{}`", key)),
53+
definition: definition,
54+
}
55+
}
56+
}
57+
58+
impl std::error::Error for ConfigError {
59+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
60+
self.error.source()
61+
}
62+
}
63+
64+
impl fmt::Display for ConfigError {
65+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66+
if let Some(definition) = &self.definition {
67+
write!(f, "error in {}: {}", definition, self.error)
68+
} else {
69+
self.error.fmt(f)
70+
}
71+
}
72+
}
73+
74+
#[derive(Debug)]
75+
struct MissingFieldError(String);
76+
77+
impl fmt::Display for MissingFieldError {
78+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79+
write!(f, "missing field `{}`", self.0)
80+
}
81+
}
82+
83+
impl std::error::Error for MissingFieldError {}
84+
85+
impl serde::de::Error for ConfigError {
86+
fn custom<T: fmt::Display>(msg: T) -> Self {
87+
ConfigError {
88+
error: anyhow::Error::msg(msg.to_string()),
89+
definition: None,
90+
}
91+
}
92+
93+
fn missing_field(field: &'static str) -> Self {
94+
ConfigError {
95+
error: anyhow::Error::new(MissingFieldError(field.to_string())),
96+
definition: None,
97+
}
98+
}
99+
}
100+
101+
impl From<anyhow::Error> for ConfigError {
102+
fn from(error: anyhow::Error) -> Self {
103+
ConfigError {
104+
error,
105+
definition: None,
106+
}
107+
}
108+
}

src/cargo/util/context/mod.rs

Lines changed: 3 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ use url::Url;
9696
mod de;
9797
use de::Deserializer;
9898

99+
mod error;
100+
pub use error::ConfigError;
101+
99102
mod value;
100103
pub use value::{Definition, OptValue, Value};
101104

@@ -2052,105 +2055,6 @@ impl GlobalContext {
20522055
}
20532056
}
20542057

2055-
/// Internal error for serde errors.
2056-
#[derive(Debug)]
2057-
pub struct ConfigError {
2058-
error: anyhow::Error,
2059-
definition: Option<Definition>,
2060-
}
2061-
2062-
impl ConfigError {
2063-
fn new(message: String, definition: Definition) -> ConfigError {
2064-
ConfigError {
2065-
error: anyhow::Error::msg(message),
2066-
definition: Some(definition),
2067-
}
2068-
}
2069-
2070-
fn expected(key: &ConfigKey, expected: &str, found: &ConfigValue) -> ConfigError {
2071-
ConfigError {
2072-
error: anyhow!(
2073-
"`{}` expected {}, but found a {}",
2074-
key,
2075-
expected,
2076-
found.desc()
2077-
),
2078-
definition: Some(found.definition().clone()),
2079-
}
2080-
}
2081-
2082-
fn is_missing_field(&self) -> bool {
2083-
self.error.downcast_ref::<MissingFieldError>().is_some()
2084-
}
2085-
2086-
fn missing(key: &ConfigKey) -> ConfigError {
2087-
ConfigError {
2088-
error: anyhow!("missing config key `{}`", key),
2089-
definition: None,
2090-
}
2091-
}
2092-
2093-
fn with_key_context(self, key: &ConfigKey, definition: Option<Definition>) -> ConfigError {
2094-
ConfigError {
2095-
error: anyhow::Error::from(self)
2096-
.context(format!("could not load config key `{}`", key)),
2097-
definition: definition,
2098-
}
2099-
}
2100-
}
2101-
2102-
impl std::error::Error for ConfigError {
2103-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
2104-
self.error.source()
2105-
}
2106-
}
2107-
2108-
impl fmt::Display for ConfigError {
2109-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2110-
if let Some(definition) = &self.definition {
2111-
write!(f, "error in {}: {}", definition, self.error)
2112-
} else {
2113-
self.error.fmt(f)
2114-
}
2115-
}
2116-
}
2117-
2118-
#[derive(Debug)]
2119-
struct MissingFieldError(String);
2120-
2121-
impl fmt::Display for MissingFieldError {
2122-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2123-
write!(f, "missing field `{}`", self.0)
2124-
}
2125-
}
2126-
2127-
impl std::error::Error for MissingFieldError {}
2128-
2129-
impl serde::de::Error for ConfigError {
2130-
fn custom<T: fmt::Display>(msg: T) -> Self {
2131-
ConfigError {
2132-
error: anyhow::Error::msg(msg.to_string()),
2133-
definition: None,
2134-
}
2135-
}
2136-
2137-
fn missing_field(field: &'static str) -> Self {
2138-
ConfigError {
2139-
error: anyhow::Error::new(MissingFieldError(field.to_string())),
2140-
definition: None,
2141-
}
2142-
}
2143-
}
2144-
2145-
impl From<anyhow::Error> for ConfigError {
2146-
fn from(error: anyhow::Error) -> Self {
2147-
ConfigError {
2148-
error,
2149-
definition: None,
2150-
}
2151-
}
2152-
}
2153-
21542058
#[derive(Debug)]
21552059
enum KeyOrIdx {
21562060
Key(String),

0 commit comments

Comments
 (0)