Skip to main content

navree_kernel/
error.rs

1//! Kernel errors. Every wiring error names the module and the type involved.
2
3use std::fmt;
4
5use crate::BoxError;
6
7/// An error raised while validating the module graph, resolving a provider,
8/// running a lifecycle hook or dispatching a command.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum KernelError {
12    /// No provider for `requested` is visible from `module`.
13    #[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        /// The module doing the resolving.
18        module: &'static str,
19        /// The requested provider key.
20        requested: String,
21        /// What needed it (a provider, `exports`, a lifecycle registration, ...).
22        required_by: String,
23    },
24    /// The provider exists in an imported module but that module does not export it.
25    #[error(
26        "module `{module}` cannot use `{requested}`: it is private to module `{owner}`; add it to the exports of `{owner}`"
27    )]
28    NotExported {
29        /// The module doing the resolving.
30        module: &'static str,
31        /// The requested provider key.
32        requested: String,
33        /// The module that registers the provider.
34        owner: &'static str,
35    },
36    /// The provider is exported by a module that the requesting module does not import.
37    #[error(
38        "module `{module}` cannot use `{requested}`: it is exported by module `{owner}`, which `{module}` does not import"
39    )]
40    NotImported {
41        /// The module doing the resolving.
42        module: &'static str,
43        /// The requested provider key.
44        requested: String,
45        /// The module that registers and exports the provider.
46        owner: &'static str,
47    },
48    /// Several different providers of the key are visible (e.g. two imports export it).
49    #[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        /// The module doing the resolving.
55        module: &'static str,
56        /// The requested provider key.
57        requested: String,
58        /// The modules that own the competing providers.
59        owners: Vec<&'static str>,
60    },
61    /// A provider dependency cycle (`A -> B -> A`).
62    #[error("provider dependency cycle: {}", path.join(" -> "))]
63    ProviderCycle {
64        /// The providers on the cycle; the first and last entries are the same.
65        path: Vec<String>,
66    },
67    /// A module import cycle (`AModule -> BModule -> AModule`).
68    #[error("module import cycle: {}", path.join(" -> "))]
69    ModuleCycle {
70        /// The modules on the cycle; the first and last entries are the same.
71        path: Vec<&'static str>,
72    },
73    /// A singleton depends (directly or through transients) on a request-scoped provider.
74    #[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        /// The module that registers the singleton.
79        module: &'static str,
80        /// The singleton provider.
81        singleton: String,
82        /// The request-scoped provider it reaches.
83        request_scoped: String,
84    },
85    /// A request-scoped provider was resolved without a [`RequestScope`](crate::RequestScope).
86    #[error(
87        "request-scoped provider `{provider}` was resolved outside a request scope; resolve it through `AppContext::request_resolver`"
88    )]
89    MissingRequestScope {
90        /// The request-scoped provider.
91        provider: String,
92    },
93    /// Two configured (`for_root`) instances of the same module.
94    #[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        /// The module configured twice.
99        module: &'static str,
100    },
101    /// The same provider key registered twice in one module.
102    #[error("module `{module}` registers provider `{key}` more than once")]
103    DuplicateProvider {
104        /// The module.
105        module: &'static str,
106        /// The duplicated key.
107        key: String,
108    },
109    /// A provider factory or constructor failed.
110    #[error("failed to construct `{provider}`: {source}")]
111    Construction {
112        /// The provider being constructed.
113        provider: String,
114        /// The underlying error.
115        source: BoxError,
116    },
117    /// No command with this name is registered.
118    #[error("unknown command `{name}`; run `help` to list the available commands")]
119    UnknownCommand {
120        /// The requested command name.
121        name: String,
122    },
123    /// Two modules register a command with the same name.
124    #[error("command `{name}` is registered by both `{first}` and `{second}`")]
125    DuplicateCommand {
126        /// The command name.
127        name: String,
128        /// The module that registered it first.
129        first: &'static str,
130        /// The module that registered it again.
131        second: &'static str,
132    },
133    /// A lifecycle hook failed.
134    #[error("`{hook}` hook of module `{module}` failed: {source}")]
135    Lifecycle {
136        /// The module whose provider implements the hook.
137        module: &'static str,
138        /// `on_init`, `on_bootstrap` or `on_shutdown`.
139        hook: &'static str,
140        /// The underlying error.
141        source: BoxError,
142    },
143}
144
145/// Every error found while booting or validating an app.
146#[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}