1use std::fmt;
4
5use crate::BoxError;
6
7#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum KernelError {
12 #[error(
14 "module `{module}` cannot resolve `{requested}` (required by {required_by}): no provider is registered in `{module}`, exported by a module it imports, or exported by a global module"
15 )]
16 MissingProvider {
17 module: &'static str,
19 requested: String,
21 required_by: String,
23 },
24 #[error(
26 "module `{module}` cannot use `{requested}`: it is private to module `{owner}`; add it to the exports of `{owner}`"
27 )]
28 NotExported {
29 module: &'static str,
31 requested: String,
33 owner: &'static str,
35 },
36 #[error(
38 "module `{module}` cannot use `{requested}`: it is exported by module `{owner}`, which `{module}` does not import"
39 )]
40 NotImported {
41 module: &'static str,
43 requested: String,
45 owner: &'static str,
47 },
48 #[error(
50 "module `{module}` sees `{requested}` from several modules ({}); export it from one module only, or use named providers",
51 owners.join(", ")
52 )]
53 AmbiguousProvider {
54 module: &'static str,
56 requested: String,
58 owners: Vec<&'static str>,
60 },
61 #[error("provider dependency cycle: {}", path.join(" -> "))]
63 ProviderCycle {
64 path: Vec<String>,
66 },
67 #[error("module import cycle: {}", path.join(" -> "))]
69 ModuleCycle {
70 path: Vec<&'static str>,
72 },
73 #[error(
75 "scope inversion in module `{module}`: singleton `{singleton}` depends on request-scoped `{request_scoped}`; take request values as parameters or extractors instead"
76 )]
77 ScopeInversion {
78 module: &'static str,
80 singleton: String,
82 request_scoped: String,
84 },
85 #[error(
87 "request-scoped provider `{provider}` was resolved outside a request scope; resolve it through `AppContext::request_resolver`"
88 )]
89 MissingRequestScope {
90 provider: String,
92 },
93 #[error(
95 "module `{module}` is configured more than once; call `for_root` once (usually in the app root) and import the module by type elsewhere"
96 )]
97 ConfiguredTwice {
98 module: &'static str,
100 },
101 #[error("module `{module}` registers provider `{key}` more than once")]
103 DuplicateProvider {
104 module: &'static str,
106 key: String,
108 },
109 #[error("failed to construct `{provider}`: {source}")]
111 Construction {
112 provider: String,
114 source: BoxError,
116 },
117 #[error("unknown command `{name}`; run `help` to list the available commands")]
119 UnknownCommand {
120 name: String,
122 },
123 #[error("command `{name}` is registered by both `{first}` and `{second}`")]
125 DuplicateCommand {
126 name: String,
128 first: &'static str,
130 second: &'static str,
132 },
133 #[error("`{hook}` hook of module `{module}` failed: {source}")]
135 Lifecycle {
136 module: &'static str,
138 hook: &'static str,
140 source: BoxError,
142 },
143}
144
145#[derive(Debug)]
147pub struct BootErrors(pub Vec<KernelError>);
148
149impl fmt::Display for BootErrors {
150 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151 for (i, error) in self.0.iter().enumerate() {
152 if i > 0 {
153 f.write_str("\n")?;
154 }
155 write!(f, "{error}")?;
156 }
157 Ok(())
158 }
159}
160
161impl std::error::Error for BootErrors {}
162
163impl From<KernelError> for BootErrors {
164 fn from(error: KernelError) -> Self {
165 Self(vec![error])
166 }
167}