grafbase_sdk/component/
error.rs

1use crate::wit;
2
3/// Internal SDK error.
4#[derive(Debug)]
5pub struct SdkError(SdkErrorInner);
6
7impl std::fmt::Display for SdkError {
8    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
9        write!(f, "{}", self.0)
10    }
11}
12
13impl std::error::Error for SdkError {
14    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
15        self.0.source()
16    }
17}
18
19#[derive(Debug, thiserror::Error)]
20pub(crate) enum SdkErrorInner {
21    #[error("{0}")]
22    Message(String),
23    #[error("Serialization failed with: {0}")]
24    EncodeError(#[from] minicbor_serde::error::EncodeError<std::convert::Infallible>),
25    #[error("Deserialization failed with: {0}")]
26    DecodeError(#[from] minicbor_serde::error::DecodeError),
27    #[error("JSON serialization error: {0}")]
28    Json(#[from] serde_json::Error),
29    #[error("{0}")]
30    WitError(#[from] wit::Error),
31}
32
33impl<T> From<T> for SdkError
34where
35    SdkErrorInner: From<T>,
36{
37    fn from(err: T) -> Self {
38        Self(err.into())
39    }
40}
41
42impl From<String> for SdkErrorInner {
43    fn from(err: String) -> Self {
44        Self::Message(err)
45    }
46}
47
48impl From<&'static str> for SdkErrorInner {
49    fn from(err: &'static str) -> Self {
50        Self::Message(err.to_string())
51    }
52}
53
54impl From<SdkError> for wit::Error {
55    fn from(err: SdkError) -> Self {
56        wit::Error::new(err.to_string())
57    }
58}