Skip to main content

spark/
error.rs

1//! Spark error type.
2
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum Error {
7    #[error("unknown component: {0}")]
8    UnknownComponent(String),
9
10    #[error("unknown method `{method}` on component `{class}`")]
11    UnknownMethod { class: String, method: String },
12
13    #[error("invalid arguments for `{method}`: {message}")]
14    InvalidArguments { method: String, message: String },
15
16    #[error("snapshot decode failed: {0}")]
17    SnapshotDecode(String),
18
19    #[error("snapshot checksum mismatch")]
20    SnapshotTampered,
21
22    #[error("snapshot too large: {size} bytes (max {max})")]
23    SnapshotTooLarge { size: usize, max: usize },
24
25    /// The submitted snapshot revision is not the latest one this server
26    /// issued for the component instance. The client should reload to pick up
27    /// the current state.
28    #[error("snapshot is stale: server has rev {server_rev}, client sent rev {client_rev}")]
29    SnapshotStale { server_rev: u64, client_rev: u64 },
30
31    /// The submitted snapshot is in a newer wire-format version than this
32    /// build understands. Maps to HTTP 426 Upgrade Required so the client
33    /// fetches the new asset on next refresh.
34    #[error(
35        "snapshot wire-format version {client_v} is newer than this server understands ({server_v}) — refresh the page"
36    )]
37    SnapshotVersionMismatch { client_v: u8, server_v: u8 },
38
39    #[error("template error: {0}")]
40    Template(String),
41
42    #[error("io: {0}")]
43    Io(#[from] std::io::Error),
44
45    #[error("serde: {0}")]
46    Serde(#[from] serde_json::Error),
47}
48
49pub type Result<T> = std::result::Result<T, Error>;
50
51impl From<Error> for anvil_core::Error {
52    fn from(e: Error) -> Self {
53        match &e {
54            Error::SnapshotTampered => anvil_core::Error::forbidden("snapshot tampered"),
55            Error::SnapshotTooLarge { .. } => anvil_core::Error::bad_request(format!("{e}")),
56            Error::SnapshotStale { .. } => anvil_core::Error::Conflict(format!("{e}")),
57            // Fallback mapping — the http.rs handler catches this case
58            // earlier and returns HTTP 426 directly. Anything that reaches
59            // here lacked that path; surface it as a 409 so the client at
60            // least knows the snapshot is unusable.
61            Error::SnapshotVersionMismatch { .. } => anvil_core::Error::Conflict(format!("{e}")),
62            Error::SnapshotDecode(msg) => {
63                anvil_core::Error::bad_request(format!("snapshot decode: {msg}"))
64            }
65            Error::UnknownComponent(c) => {
66                anvil_core::Error::bad_request(format!("unknown component: {c}"))
67            }
68            Error::UnknownMethod { class, method } => {
69                anvil_core::Error::bad_request(format!("unknown method `{method}` on `{class}`"))
70            }
71            Error::InvalidArguments { method, message } => {
72                anvil_core::Error::bad_request(format!("invalid args for {method}: {message}"))
73            }
74            Error::Template(t) => anvil_core::Error::Internal(format!("template: {t}")),
75            Error::Io(_) | Error::Serde(_) => anvil_core::Error::Internal(format!("{e}")),
76        }
77    }
78}