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    /// A module calls `m.plugin(..)` more than once.
144    #[error("module `{module}` calls m.plugin(..) twice; call it once in its register")]
145    PluginTwice {
146        /// The module.
147        module: &'static str,
148    },
149    /// A plugin's namespace is not a lower-case letter followed by up to 63
150    /// lower-case letters, digits or `_`.
151    #[error(
152        "plugin `{plugin}` in module `{module}` has the namespace `{namespace}`, which is not a lower-case letter followed by up to 63 lower-case letters, digits or _; set one with plugin_info!(namespace = \"..\")"
153    )]
154    InvalidNamespace {
155        /// The module that names the plugin.
156        module: &'static str,
157        /// The plugin's name.
158        plugin: &'static str,
159        /// The namespace it gives.
160        namespace: &'static str,
161    },
162    /// A module names a plugin of another crate: its type names no path of the
163    /// plugin's crate, or another module gives the plugin another crate.
164    #[error(
165        "module `{module}` names the plugin `{plugin}`, which is another crate's; a plugin's modules are in its own crate (a ClientModule names it through an options type of the plugin's crate)"
166    )]
167    PluginOfAnotherCrate {
168        /// The module.
169        module: &'static str,
170        /// The plugin's name.
171        plugin: &'static str,
172    },
173    /// A plugin that is not first-party uses a namespace the framework keeps.
174    #[error(
175        "plugin `{plugin}` uses the namespace `{namespace}`, which the framework keeps for itself; remove the plugin or update it to a version that uses another namespace"
176    )]
177    NamespaceReserved {
178        /// The plugin's name.
179        plugin: &'static str,
180        /// The reserved namespace.
181        namespace: &'static str,
182    },
183    /// Two modules name one plugin at two versions.
184    #[error(
185        "plugin `{plugin}` is in the app at two versions ({first} in `{first_module}`, {second} in `{second_module}`); depend on one version"
186    )]
187    PluginVersions {
188        /// The plugin's name.
189        plugin: &'static str,
190        /// The version the first module gives.
191        first: &'static str,
192        /// The first module.
193        first_module: &'static str,
194        /// The other version.
195        second: &'static str,
196        /// The module that gives it.
197        second_module: &'static str,
198    },
199    /// Two modules describe one plugin (one name, crate and version) differently.
200    #[error(
201        "plugin `{plugin}` is described differently by `{first_module}` and `{second_module}`; give every module of a plugin the same plugin_info!(..)"
202    )]
203    PluginInfoDiffers {
204        /// The plugin's name.
205        plugin: &'static str,
206        /// The module whose description counts.
207        first_module: &'static str,
208        /// A module that describes the plugin otherwise.
209        second_module: &'static str,
210    },
211    /// Two plugins use one namespace.
212    #[error(
213        "plugins `{first}` and `{second}` both use the namespace `{namespace}`; remove one of the plugins or update one to a version that uses another namespace"
214    )]
215    NamespaceTaken {
216        /// The namespace.
217        namespace: &'static str,
218        /// The plugin that uses it first.
219        first: &'static str,
220        /// The plugin that uses it too.
221        second: &'static str,
222    },
223    /// A module that does not name itself a plugin has files to publish.
224    #[error(
225        "module `{module}` has files to publish but does not name itself a plugin; call m.plugin(plugin_info!()) in its register"
226    )]
227    PublishableOutsidePlugin {
228        /// The module.
229        module: &'static str,
230    },
231    /// A file a plugin ships has a path that is not safe inside its folder.
232    #[error(
233        "plugin `{plugin}`: the {tag} file `{path}` is not a safe relative path inside its folder (no `..`, no leading `/`, no `\\`, no `:`, none of `<>\"|?*`, no segment ending in `.` or a space, no device name such as `con` or `nul`, no two names that differ only in case, no file that is also a folder); fix the path in the plugin"
234    )]
235    UnsafePublishPath {
236        /// The plugin's name.
237        plugin: &'static str,
238        /// The file's tag: `views`, `locales` or `files`.
239        tag: &'static str,
240        /// The file's path, as the plugin gives it.
241        path: String,
242    },
243    /// A module that acts for a plugin that is not first-party imports a
244    /// configured instance of a module whose type does not name the plugin's crate.
245    #[error(
246        "plugin `{plugin}` configures the module `{module}` in `{importer}`{reason} (for_root or from_config); only a module of the app configures a module of another crate: remove the plugin or update it to a version that imports `{module}` without configuring it, and configure `{module}` in a module of the app"
247    )]
248    PluginConfiguresModule {
249        /// The plugin's name.
250        plugin: &'static str,
251        /// The configured module.
252        module: &'static str,
253        /// The module that imports the configured instance.
254        importer: &'static str,
255        /// Why `importer` acts for the plugin when it does not name it, in
256        /// parentheses after a space; empty when it names it.
257        reason: String,
258    },
259    /// A module that acts for a plugin that is not first-party registers a
260    /// provider that only the app registers: one that decides who may do what
261    /// in the app, the options of such a module, the app's shell, its clock,
262    /// its audit sink, or an observer of every commit. The text ends with how
263    /// the app uses the plugin's: a module of the app registers it (or adds the
264    /// audit sink with `ModuleBuilderAuditExt`).
265    #[error(
266        "plugin `{plugin}` provides `{provider}` in `{module}`{reason}, {role}; only a module of the app registers it: remove the plugin or update it to a version that leaves it to the app, {}",
267        crate::plugin::app_only_advice(.provider)
268    )]
269    PluginProvidesAppOnly {
270        /// The plugin's name.
271        plugin: &'static str,
272        /// The provider's type name.
273        provider: &'static str,
274        /// What the provider does in the app, as a clause after its name.
275        role: &'static str,
276        /// The module that registers it.
277        module: &'static str,
278        /// Why `module` acts for the plugin when it does not name it, in
279        /// parentheses after a space; empty when it names it.
280        reason: String,
281    },
282    /// A module that acts for a plugin that is not first-party registers a
283    /// command with the name of one of the framework's own.
284    #[error(
285        "plugin `{plugin}` registers the command `{command}` in `{module}`{reason}, a command of the framework's own; only a module of the app may take its name: remove the plugin or update it to a version that names its command otherwise"
286    )]
287    PluginCommand {
288        /// The plugin's name.
289        plugin: &'static str,
290        /// The command's name.
291        command: &'static str,
292        /// The module that registers it.
293        module: &'static str,
294        /// Why `module` acts for the plugin when it does not name it, in
295        /// parentheses after a space; empty when it names it.
296        reason: String,
297    },
298    /// A module that acts for a plugin that is not first-party hooks a model of
299    /// another crate.
300    #[error(
301        "plugin `{plugin}` hooks the model `{model}` in `{module}`{reason}, a model of another crate; only a module of the app hooks it: remove the plugin or update it to a version that leaves the hook to the app, and to use the plugin's hook, register it from a module of the app"
302    )]
303    PluginHooksModel {
304        /// The plugin's name.
305        plugin: &'static str,
306        /// The model's type path.
307        model: &'static str,
308        /// The module that registers the hook.
309        module: &'static str,
310        /// Why `module` acts for the plugin when it does not name it, in
311        /// parentheses after a space; empty when it names it.
312        reason: String,
313    },
314    /// Two module values from different crates return the same [`ModuleId`](crate::ModuleId).
315    #[error(
316        "module id `{module}` (`{id_type}`) is claimed by modules in two crates (`{first}` and `{second}`); each module id belongs to one crate"
317    )]
318    DuplicateModuleId {
319        /// The readable name of the module id.
320        module: &'static str,
321        /// The type path recorded in the module id.
322        id_type: &'static str,
323        /// The full type path of the first module value that returned the id.
324        first: &'static str,
325        /// The full type path of the other crate's module value that returned the id.
326        second: &'static str,
327    },
328    /// A module that acts for a plugin that is not first-party exports a
329    /// non-multi provider key that another module also registers, which would
330    /// either replace that module's unexported provider in app-wide lookups or
331    /// make app-wide lookups of the key answer `None`.
332    #[error("{}", plugin_exports_provider_message(.plugin, .provider, .module, .reason, .other, *.also_exported))]
333    PluginExportsProvider {
334        /// The plugin's name.
335        plugin: &'static str,
336        /// The exported provider key.
337        provider: String,
338        /// The plugin's module that exports it.
339        module: &'static str,
340        /// Why `module` acts for the plugin when it does not name it, in
341        /// parentheses after a space; empty when it names it.
342        reason: String,
343        /// Another module that registers the same key.
344        other: &'static str,
345        /// Whether `other` also exports the key.
346        also_exported: bool,
347    },
348    /// A module that acts for a plugin that is not first-party exports a
349    /// non-multi provider whose outer type belongs to a framework crate and
350    /// names no type of the plugin's own crate.
351    #[error(
352        "plugin `{plugin}` exports `{provider}` in `{module}`{reason}, a provider type of the framework; only a framework or app module exports `{provider}`: remove the plugin or update it to a version that leaves `{provider}` to the framework and registers its drivers or extensions through the module builder"
353    )]
354    PluginExportsFrameworkProvider {
355        /// The plugin's name.
356        plugin: &'static str,
357        /// The exported provider key.
358        provider: String,
359        /// The plugin's module that exports it.
360        module: &'static str,
361        /// Why `module` acts for the plugin when it does not name it, in
362        /// parentheses after a space; empty when it names it.
363        reason: String,
364    },
365}
366
367fn plugin_exports_provider_message(
368    plugin: &str,
369    provider: &str,
370    module: &str,
371    reason: &str,
372    other: &str,
373    also_exported: bool,
374) -> String {
375    if also_exported {
376        format!(
377            "plugin `{plugin}` exports `{provider}` in `{module}`{reason}, which `{other}` also exports; two exports of one provider make app-wide lookups of `{provider}` answer nothing: remove the plugin or update it to a version that keeps `{provider}` private to `{module}` or registers it under a name"
378        )
379    } else {
380        format!(
381            "plugin `{plugin}` exports `{provider}` in `{module}`{reason}, which `{other}` also provides; exporting `{provider}` would replace `{other}`'s provider in app-wide lookups: remove the plugin or update it to a version that keeps `{provider}` private to `{module}` or registers it under a name"
382        )
383    }
384}
385
386/// Every error found while booting or validating an app.
387#[derive(Debug)]
388pub struct BootErrors(pub Vec<KernelError>);
389
390impl fmt::Display for BootErrors {
391    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
392        for (i, error) in self.0.iter().enumerate() {
393            if i > 0 {
394                f.write_str("\n")?;
395            }
396            write!(f, "{error}")?;
397        }
398        Ok(())
399    }
400}
401
402impl std::error::Error for BootErrors {}
403
404impl From<KernelError> for BootErrors {
405    fn from(error: KernelError) -> Self {
406        Self(vec![error])
407    }
408}