Skip to main content

greentic_component/
error.rs

1use displaydoc::Display;
2
3#[cfg(feature = "abi")]
4use crate::abi::AbiError;
5use crate::capabilities::CapabilityError;
6#[cfg(feature = "describe")]
7use crate::describe::DescribeError;
8use crate::limits::LimitError;
9#[cfg(feature = "loader")]
10use crate::loader::LoadError;
11use crate::manifest::ManifestError;
12use crate::schema::SchemaIntrospectionError;
13use crate::signing::SigningError;
14
15#[derive(Debug, Display, thiserror::Error)]
16pub enum ComponentError {
17    /// manifest failure: {0}
18    Manifest(#[from] ManifestError),
19    /// schema introspection failure: {0}
20    SchemaIntrospection(#[from] SchemaIntrospectionError),
21    /// ABI failure: {0}
22    #[cfg(feature = "abi")]
23    Abi(#[from] AbiError),
24    /// describe failure: {0}
25    #[cfg(feature = "describe")]
26    Describe(#[from] DescribeError),
27    /// load failure: {0}
28    #[cfg(feature = "loader")]
29    Load(#[from] LoadError),
30    /// capability failure: {0}
31    Capability(#[from] CapabilityError),
32    /// limit failure: {0}
33    Limits(#[from] LimitError),
34    /// signing failure: {0}
35    Signing(#[from] SigningError),
36    /// io failure: {0}
37    Io(#[from] std::io::Error),
38    /// doctor failure: {0}
39    Doctor(String),
40    /// operation schema empty: {component}/{operation} {direction} ({suggestion})
41    SchemaQualityEmpty {
42        component: String,
43        operation: String,
44        direction: &'static str,
45        suggestion: String,
46    },
47}
48
49impl ComponentError {
50    pub fn code(&self) -> &'static str {
51        match self {
52            ComponentError::Manifest(_) => "manifest-invalid",
53            ComponentError::SchemaIntrospection(_) => "schema-introspection",
54            #[cfg(feature = "abi")]
55            ComponentError::Abi(err) => match err {
56                crate::abi::AbiError::WorldMismatch { .. } => "world-mismatch",
57                crate::abi::AbiError::MissingWasiTarget => "wasi-target-missing",
58                _ => "abi-error",
59            },
60            #[cfg(feature = "describe")]
61            ComponentError::Describe(err) => match err {
62                crate::describe::DescribeError::NotFound(_) => "describe-missing",
63                crate::describe::DescribeError::Json { .. } => "describe-invalid",
64                crate::describe::DescribeError::Io { .. } => "describe-io",
65                crate::describe::DescribeError::Metadata(_) => "describe-metadata",
66            },
67            #[cfg(feature = "loader")]
68            ComponentError::Load(_) => "component-load",
69            ComponentError::Capability(_) => "capability-error",
70            ComponentError::Limits(_) => "limits-error",
71            ComponentError::Signing(_) => "hash-mismatch",
72            ComponentError::Io(_) => "io-error",
73            ComponentError::Doctor(_) => "doctor-failure",
74            ComponentError::SchemaQualityEmpty { .. } => "E_OP_SCHEMA_EMPTY",
75        }
76    }
77}