1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Kernel errors. Every wiring error names the module and the type involved.
use std::fmt;
use crate::BoxError;
/// An error raised while validating the module graph, resolving a provider,
/// running a lifecycle hook or dispatching a command.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum KernelError {
/// No provider for `requested` is visible from `module`.
#[error(
"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"
)]
MissingProvider {
/// The module doing the resolving.
module: &'static str,
/// The requested provider key.
requested: String,
/// What needed it (a provider, `exports`, a lifecycle registration, ...).
required_by: String,
},
/// The provider exists in an imported module but that module does not export it.
#[error(
"module `{module}` cannot use `{requested}`: it is private to module `{owner}`; add it to the exports of `{owner}`"
)]
NotExported {
/// The module doing the resolving.
module: &'static str,
/// The requested provider key.
requested: String,
/// The module that registers the provider.
owner: &'static str,
},
/// The provider is exported by a module that the requesting module does not import.
#[error(
"module `{module}` cannot use `{requested}`: it is exported by module `{owner}`, which `{module}` does not import"
)]
NotImported {
/// The module doing the resolving.
module: &'static str,
/// The requested provider key.
requested: String,
/// The module that registers and exports the provider.
owner: &'static str,
},
/// Several different providers of the key are visible (e.g. two imports export it).
#[error(
"module `{module}` sees `{requested}` from several modules ({}); export it from one module only, or use named providers",
owners.join(", ")
)]
AmbiguousProvider {
/// The module doing the resolving.
module: &'static str,
/// The requested provider key.
requested: String,
/// The modules that own the competing providers.
owners: Vec<&'static str>,
},
/// A provider dependency cycle (`A -> B -> A`).
#[error("provider dependency cycle: {}", path.join(" -> "))]
ProviderCycle {
/// The providers on the cycle; the first and last entries are the same.
path: Vec<String>,
},
/// A module import cycle (`AModule -> BModule -> AModule`).
#[error("module import cycle: {}", path.join(" -> "))]
ModuleCycle {
/// The modules on the cycle; the first and last entries are the same.
path: Vec<&'static str>,
},
/// A singleton depends (directly or through transients) on a request-scoped provider.
#[error(
"scope inversion in module `{module}`: singleton `{singleton}` depends on request-scoped `{request_scoped}`; take request values as parameters or extractors instead"
)]
ScopeInversion {
/// The module that registers the singleton.
module: &'static str,
/// The singleton provider.
singleton: String,
/// The request-scoped provider it reaches.
request_scoped: String,
},
/// A request-scoped provider was resolved without a [`RequestScope`](crate::RequestScope).
#[error(
"request-scoped provider `{provider}` was resolved outside a request scope; resolve it through `AppContext::request_resolver`"
)]
MissingRequestScope {
/// The request-scoped provider.
provider: String,
},
/// Two configured (`for_root`) instances of the same module.
#[error(
"module `{module}` is configured more than once; call `for_root` once (usually in the app root) and import the module by type elsewhere"
)]
ConfiguredTwice {
/// The module configured twice.
module: &'static str,
},
/// The same provider key registered twice in one module.
#[error("module `{module}` registers provider `{key}` more than once")]
DuplicateProvider {
/// The module.
module: &'static str,
/// The duplicated key.
key: String,
},
/// A provider factory or constructor failed.
#[error("failed to construct `{provider}`: {source}")]
Construction {
/// The provider being constructed.
provider: String,
/// The underlying error.
source: BoxError,
},
/// No command with this name is registered.
#[error("unknown command `{name}`; run `help` to list the available commands")]
UnknownCommand {
/// The requested command name.
name: String,
},
/// Two modules register a command with the same name.
#[error("command `{name}` is registered by both `{first}` and `{second}`")]
DuplicateCommand {
/// The command name.
name: String,
/// The module that registered it first.
first: &'static str,
/// The module that registered it again.
second: &'static str,
},
/// A lifecycle hook failed.
#[error("`{hook}` hook of module `{module}` failed: {source}")]
Lifecycle {
/// The module whose provider implements the hook.
module: &'static str,
/// `on_init`, `on_bootstrap` or `on_shutdown`.
hook: &'static str,
/// The underlying error.
source: BoxError,
},
/// A module calls `m.plugin(..)` more than once.
#[error("module `{module}` calls m.plugin(..) twice; call it once in its register")]
PluginTwice {
/// The module.
module: &'static str,
},
/// A plugin's namespace is not a lower-case letter followed by up to 63
/// lower-case letters, digits or `_`.
#[error(
"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 = \"..\")"
)]
InvalidNamespace {
/// The module that names the plugin.
module: &'static str,
/// The plugin's name.
plugin: &'static str,
/// The namespace it gives.
namespace: &'static str,
},
/// A module names a plugin of another crate: its type names no path of the
/// plugin's crate, or another module gives the plugin another crate.
#[error(
"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)"
)]
PluginOfAnotherCrate {
/// The module.
module: &'static str,
/// The plugin's name.
plugin: &'static str,
},
/// A plugin that is not first-party uses a namespace the framework keeps.
#[error(
"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"
)]
NamespaceReserved {
/// The plugin's name.
plugin: &'static str,
/// The reserved namespace.
namespace: &'static str,
},
/// Two modules name one plugin at two versions.
#[error(
"plugin `{plugin}` is in the app at two versions ({first} in `{first_module}`, {second} in `{second_module}`); depend on one version"
)]
PluginVersions {
/// The plugin's name.
plugin: &'static str,
/// The version the first module gives.
first: &'static str,
/// The first module.
first_module: &'static str,
/// The other version.
second: &'static str,
/// The module that gives it.
second_module: &'static str,
},
/// Two modules describe one plugin (one name, crate and version) differently.
#[error(
"plugin `{plugin}` is described differently by `{first_module}` and `{second_module}`; give every module of a plugin the same plugin_info!(..)"
)]
PluginInfoDiffers {
/// The plugin's name.
plugin: &'static str,
/// The module whose description counts.
first_module: &'static str,
/// A module that describes the plugin otherwise.
second_module: &'static str,
},
/// Two plugins use one namespace.
#[error(
"plugins `{first}` and `{second}` both use the namespace `{namespace}`; remove one of the plugins or update one to a version that uses another namespace"
)]
NamespaceTaken {
/// The namespace.
namespace: &'static str,
/// The plugin that uses it first.
first: &'static str,
/// The plugin that uses it too.
second: &'static str,
},
/// A module that does not name itself a plugin has files to publish.
#[error(
"module `{module}` has files to publish but does not name itself a plugin; call m.plugin(plugin_info!()) in its register"
)]
PublishableOutsidePlugin {
/// The module.
module: &'static str,
},
/// A file a plugin ships has a path that is not safe inside its folder.
#[error(
"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"
)]
UnsafePublishPath {
/// The plugin's name.
plugin: &'static str,
/// The file's tag: `views`, `locales` or `files`.
tag: &'static str,
/// The file's path, as the plugin gives it.
path: String,
},
/// A module that acts for a plugin that is not first-party imports a
/// configured instance of a module whose type does not name the plugin's crate.
#[error(
"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"
)]
PluginConfiguresModule {
/// The plugin's name.
plugin: &'static str,
/// The configured module.
module: &'static str,
/// The module that imports the configured instance.
importer: &'static str,
/// Why `importer` acts for the plugin when it does not name it, in
/// parentheses after a space; empty when it names it.
reason: String,
},
/// A module that acts for a plugin that is not first-party registers a
/// provider that only the app registers: one that decides who may do what
/// in the app, the options of such a module, the app's shell, its clock,
/// its audit sink, or an observer of every commit. The text ends with how
/// the app uses the plugin's: a module of the app registers it (or adds the
/// audit sink with `ModuleBuilderAuditExt`).
#[error(
"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, {}",
crate::plugin::app_only_advice(.provider)
)]
PluginProvidesAppOnly {
/// The plugin's name.
plugin: &'static str,
/// The provider's type name.
provider: &'static str,
/// What the provider does in the app, as a clause after its name.
role: &'static str,
/// The module that registers it.
module: &'static str,
/// Why `module` acts for the plugin when it does not name it, in
/// parentheses after a space; empty when it names it.
reason: String,
},
/// A module that acts for a plugin that is not first-party registers a
/// command with the name of one of the framework's own.
#[error(
"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"
)]
PluginCommand {
/// The plugin's name.
plugin: &'static str,
/// The command's name.
command: &'static str,
/// The module that registers it.
module: &'static str,
/// Why `module` acts for the plugin when it does not name it, in
/// parentheses after a space; empty when it names it.
reason: String,
},
/// A module that acts for a plugin that is not first-party hooks a model of
/// another crate.
#[error(
"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"
)]
PluginHooksModel {
/// The plugin's name.
plugin: &'static str,
/// The model's type path.
model: &'static str,
/// The module that registers the hook.
module: &'static str,
/// Why `module` acts for the plugin when it does not name it, in
/// parentheses after a space; empty when it names it.
reason: String,
},
/// Two module values from different crates return the same [`ModuleId`](crate::ModuleId).
#[error(
"module id `{module}` (`{id_type}`) is claimed by modules in two crates (`{first}` and `{second}`); each module id belongs to one crate"
)]
DuplicateModuleId {
/// The readable name of the module id.
module: &'static str,
/// The type path recorded in the module id.
id_type: &'static str,
/// The full type path of the first module value that returned the id.
first: &'static str,
/// The full type path of the other crate's module value that returned the id.
second: &'static str,
},
/// A module that acts for a plugin that is not first-party exports a
/// non-multi provider key that another module also registers, which would
/// either replace that module's unexported provider in app-wide lookups or
/// make app-wide lookups of the key answer `None`.
#[error("{}", plugin_exports_provider_message(.plugin, .provider, .module, .reason, .other, *.also_exported))]
PluginExportsProvider {
/// The plugin's name.
plugin: &'static str,
/// The exported provider key.
provider: String,
/// The plugin's module that exports it.
module: &'static str,
/// Why `module` acts for the plugin when it does not name it, in
/// parentheses after a space; empty when it names it.
reason: String,
/// Another module that registers the same key.
other: &'static str,
/// Whether `other` also exports the key.
also_exported: bool,
},
/// A module that acts for a plugin that is not first-party exports a
/// non-multi provider whose outer type belongs to a framework crate and
/// names no type of the plugin's own crate.
#[error(
"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"
)]
PluginExportsFrameworkProvider {
/// The plugin's name.
plugin: &'static str,
/// The exported provider key.
provider: String,
/// The plugin's module that exports it.
module: &'static str,
/// Why `module` acts for the plugin when it does not name it, in
/// parentheses after a space; empty when it names it.
reason: String,
},
}
fn plugin_exports_provider_message(
plugin: &str,
provider: &str,
module: &str,
reason: &str,
other: &str,
also_exported: bool,
) -> String {
if also_exported {
format!(
"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"
)
} else {
format!(
"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"
)
}
}
/// Every error found while booting or validating an app.
#[derive(Debug)]
pub struct BootErrors(pub Vec<KernelError>);
impl fmt::Display for BootErrors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, error) in self.0.iter().enumerate() {
if i > 0 {
f.write_str("\n")?;
}
write!(f, "{error}")?;
}
Ok(())
}
}
impl std::error::Error for BootErrors {}
impl From<KernelError> for BootErrors {
fn from(error: KernelError) -> Self {
Self(vec![error])
}
}