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 #[error("module `{module}` calls m.plugin(..) twice; call it once in its register")]
145 PluginTwice {
146 module: &'static str,
148 },
149 #[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 module: &'static str,
157 plugin: &'static str,
159 namespace: &'static str,
161 },
162 #[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 module: &'static str,
170 plugin: &'static str,
172 },
173 #[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 plugin: &'static str,
180 namespace: &'static str,
182 },
183 #[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 plugin: &'static str,
190 first: &'static str,
192 first_module: &'static str,
194 second: &'static str,
196 second_module: &'static str,
198 },
199 #[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 plugin: &'static str,
206 first_module: &'static str,
208 second_module: &'static str,
210 },
211 #[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 namespace: &'static str,
218 first: &'static str,
220 second: &'static str,
222 },
223 #[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 module: &'static str,
230 },
231 #[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 plugin: &'static str,
238 tag: &'static str,
240 path: String,
242 },
243 #[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 plugin: &'static str,
251 module: &'static str,
253 importer: &'static str,
255 reason: String,
258 },
259 #[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 plugin: &'static str,
272 provider: &'static str,
274 role: &'static str,
276 module: &'static str,
278 reason: String,
281 },
282 #[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 plugin: &'static str,
290 command: &'static str,
292 module: &'static str,
294 reason: String,
297 },
298 #[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 plugin: &'static str,
306 model: &'static str,
308 module: &'static str,
310 reason: String,
313 },
314 #[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 module: &'static str,
321 id_type: &'static str,
323 first: &'static str,
325 second: &'static str,
327 },
328 #[error("{}", plugin_exports_provider_message(.plugin, .provider, .module, .reason, .other, *.also_exported))]
333 PluginExportsProvider {
334 plugin: &'static str,
336 provider: String,
338 module: &'static str,
340 reason: String,
343 other: &'static str,
345 also_exported: bool,
347 },
348 #[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 plugin: &'static str,
357 provider: String,
359 module: &'static str,
361 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#[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}