Skip to main content

leviath_runtime/embed/
error.rs

1//! Errors from building or driving an embedded world.
2
3/// Why an [`AgentWorld`](super::AgentWorld) could not be built or a request to
4/// it could not be served.
5#[derive(Debug, Clone, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum EmbedError {
8    /// The builder was given no providers: no credentials and no custom
9    /// provider registrations. A world with nothing to infer against can only
10    /// error, so this fails at build time instead.
11    NoProviders,
12    /// No Tokio runtime was found. `build()` must run inside a Tokio runtime
13    /// (or be given a handle via
14    /// [`runtime`](super::AgentWorldBuilder::runtime)).
15    NoRuntime,
16    /// The blueprint could not be loaded, parsed, or validated.
17    Blueprint(String),
18    /// The spawn was rejected (bad workdir, unresolvable seeds, and so on).
19    Spawn(String),
20    /// The world's serve loop is gone (already shut down), so the request
21    /// could not be delivered or answered.
22    ChannelClosed,
23}
24
25impl std::fmt::Display for EmbedError {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        match self {
28            EmbedError::NoProviders => {
29                write!(f, "no providers configured: add credentials or a provider")
30            }
31            EmbedError::NoRuntime => {
32                write!(f, "no tokio runtime: build inside one or pass a handle")
33            }
34            EmbedError::Blueprint(msg) => write!(f, "blueprint error: {msg}"),
35            EmbedError::Spawn(msg) => write!(f, "spawn error: {msg}"),
36            EmbedError::ChannelClosed => write!(f, "the world has shut down"),
37        }
38    }
39}
40
41impl std::error::Error for EmbedError {}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn display_covers_every_variant() {
49        let cases = [
50            (EmbedError::NoProviders, "no providers"),
51            (EmbedError::NoRuntime, "no tokio runtime"),
52            (
53                EmbedError::Blueprint("bad".to_string()),
54                "blueprint error: bad",
55            ),
56            (EmbedError::Spawn("nope".to_string()), "spawn error: nope"),
57            (EmbedError::ChannelClosed, "shut down"),
58        ];
59        for (err, needle) in cases {
60            assert!(err.to_string().contains(needle));
61        }
62    }
63}