spin_plugins/
error.rs

1pub type PluginLookupResult<T> = std::result::Result<T, Error>;
2
3/// Error message during plugin lookup or deserializing
4#[derive(Debug, thiserror::Error)]
5pub enum Error {
6    #[error("{0}")]
7    NotFound(NotFoundError),
8
9    #[error("{0}")]
10    ConnectionFailed(ConnectionFailedError),
11
12    #[error("{0}")]
13    InvalidManifest(InvalidManifestError),
14
15    #[error("URL parse error {0}")]
16    UrlParseError(#[from] url::ParseError),
17
18    #[error("{0}")]
19    Other(#[from] anyhow::Error),
20}
21
22/// Contains error details for when a plugin resource cannot be found at expected location
23#[derive(Debug)]
24pub struct NotFoundError {
25    name: Option<String>,
26    addr: String,
27    err: String,
28}
29
30impl NotFoundError {
31    pub fn new(name: Option<String>, addr: String, err: String) -> Self {
32        Self { name, addr, err }
33    }
34}
35
36impl std::fmt::Display for NotFoundError {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        f.write_str(&format!(
39            "plugin '{}' not found at expected location {}: {}",
40            self.name.as_deref().unwrap_or_default(),
41            self.addr,
42            self.err
43        ))
44    }
45}
46
47/// Contains error details for when a plugin manifest cannot be properly serialized
48#[derive(Debug)]
49pub struct InvalidManifestError {
50    name: Option<String>,
51    addr: String,
52    err: String,
53}
54
55impl InvalidManifestError {
56    pub fn new(name: Option<String>, addr: String, err: String) -> Self {
57        Self { name, addr, err }
58    }
59}
60
61impl std::fmt::Display for InvalidManifestError {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        f.write_str(&format!(
64            "invalid manifest for plugin '{}' at {}: {}",
65            self.name.clone().unwrap_or_default(),
66            self.addr,
67            self.err
68        ))
69    }
70}
71
72/// Contains error details for when there is an error getting a plugin resource from an address.
73#[derive(Debug)]
74pub struct ConnectionFailedError {
75    addr: String,
76    err: String,
77}
78
79impl ConnectionFailedError {
80    pub fn new(addr: String, err: String) -> Self {
81        Self { addr, err }
82    }
83}
84
85impl std::fmt::Display for ConnectionFailedError {
86    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        f.write_str(&format!(
88            "failed to connect to endpoint {}: {}",
89            self.addr, self.err
90        ))
91    }
92}