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    /// operation schema empty: {component}/{operation} {direction} ({suggestion})
39    SchemaQualityEmpty {
40        component: String,
41        operation: String,
42        direction: &'static str,
43        suggestion: String,
44    },
45}
46
47impl ComponentError {
48    pub fn code(&self) -> &'static str {
49        match self {
50            ComponentError::Manifest(_) => "manifest-invalid",
51            ComponentError::SchemaIntrospection(_) => "schema-introspection",
52            #[cfg(feature = "abi")]
53            ComponentError::Abi(err) => match err {
54                crate::abi::AbiError::WorldMismatch { .. } => "world-mismatch",
55                crate::abi::AbiError::MissingWasiTarget => "wasi-target-missing",
56                _ => "abi-error",
57            },
58            #[cfg(feature = "describe")]
59            ComponentError::Describe(err) => match err {
60                crate::describe::DescribeError::NotFound(_) => "describe-missing",
61                crate::describe::DescribeError::Json { .. } => "describe-invalid",
62                crate::describe::DescribeError::Io { .. } => "describe-io",
63                crate::describe::DescribeError::Metadata(_) => "describe-metadata",
64            },
65            #[cfg(feature = "loader")]
66            ComponentError::Load(_) => "component-load",
67            ComponentError::Capability(_) => "capability-error",
68            ComponentError::Limits(_) => "limits-error",
69            ComponentError::Signing(_) => "hash-mismatch",
70            ComponentError::Io(_) => "io-error",
71            ComponentError::SchemaQualityEmpty { .. } => "E_OP_SCHEMA_EMPTY",
72        }
73    }
74}