Skip to main content

gg_sdk/
error.rs

1// aws-greengrass-component-sdk - Lightweight AWS IoT Greengrass SDK
2// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3// SPDX-License-Identifier: Apache-2.0
4
5use crate::c;
6use c::GgError::*;
7use core::fmt;
8
9/// GG error codes.
10#[derive(Debug, Copy, Clone, PartialEq, Eq)]
11#[repr(u32)]
12pub enum Error {
13    /// Generic failure
14    Failure = GG_ERR_FAILURE as u32,
15    /// Failure, can be retried
16    Retry = GG_ERR_RETRY as u32,
17    /// Request cannot be handled at the time
18    Busy = GG_ERR_BUSY as u32,
19    /// System is in irrecoverably broken state
20    Fatal = GG_ERR_FATAL as u32,
21    /// Request is invalid or malformed
22    Invalid = GG_ERR_INVALID as u32,
23    /// Request is unsupported
24    Unsupported = GG_ERR_UNSUPPORTED as u32,
25    /// Request data invalid
26    Parse = GG_ERR_PARSE as u32,
27    /// Request or data outside of allowable range
28    Range = GG_ERR_RANGE as u32,
29    /// Insufficient memory
30    Nomem = GG_ERR_NOMEM as u32,
31    /// No connection
32    Noconn = GG_ERR_NOCONN as u32,
33    /// No more data available
34    Nodata = GG_ERR_NODATA as u32,
35    /// Unknown entry or target requested
36    Noentry = GG_ERR_NOENTRY as u32,
37    /// Invalid or missing configuration
38    Config = GG_ERR_CONFIG as u32,
39    /// Received remote error
40    Remote = GG_ERR_REMOTE as u32,
41    /// Expected non-ok status
42    Expected = GG_ERR_EXPECTED as u32,
43    /// Request timed out
44    Timeout = GG_ERR_TIMEOUT as u32,
45    /// Lack of permission or authorization policy
46    Unauthorized = GG_ERR_UNAUTHORIZED as u32,
47    /// Version mismatch
48    Conflict = GG_ERR_CONFLICT as u32,
49}
50
51impl fmt::Display for Error {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        let s = unsafe {
54            let ptr = c::gg_strerror((*self).into());
55            let cstr = core::ffi::CStr::from_ptr(ptr.cast());
56            core::str::from_utf8_unchecked(cstr.to_bytes())
57        };
58        write!(f, "{s}")
59    }
60}
61
62/// Result type alias using SDK Error.
63pub type Result<T> = core::result::Result<T, Error>;
64
65impl From<Error> for c::GgError {
66    fn from(val: Error) -> Self {
67        match val {
68            Error::Failure => GG_ERR_FAILURE,
69            Error::Retry => GG_ERR_RETRY,
70            Error::Busy => GG_ERR_BUSY,
71            Error::Fatal => GG_ERR_FATAL,
72            Error::Invalid => GG_ERR_INVALID,
73            Error::Unsupported => GG_ERR_UNSUPPORTED,
74            Error::Parse => GG_ERR_PARSE,
75            Error::Range => GG_ERR_RANGE,
76            Error::Nomem => GG_ERR_NOMEM,
77            Error::Noconn => GG_ERR_NOCONN,
78            Error::Nodata => GG_ERR_NODATA,
79            Error::Noentry => GG_ERR_NOENTRY,
80            Error::Config => GG_ERR_CONFIG,
81            Error::Remote => GG_ERR_REMOTE,
82            Error::Expected => GG_ERR_EXPECTED,
83            Error::Timeout => GG_ERR_TIMEOUT,
84            Error::Unauthorized => GG_ERR_UNAUTHORIZED,
85            Error::Conflict => GG_ERR_CONFLICT,
86        }
87    }
88}
89
90impl From<Result<()>> for c::GgError {
91    fn from(value: Result<()>) -> Self {
92        match value {
93            Ok(()) => c::GgError::GG_ERR_OK,
94            Err(e) => e.into(),
95        }
96    }
97}
98
99impl From<c::GgError> for Result<()> {
100    fn from(err: c::GgError) -> Self {
101        match err {
102            GG_ERR_OK => Ok(()),
103            GG_ERR_FAILURE => Err(Error::Failure),
104            GG_ERR_RETRY => Err(Error::Retry),
105            GG_ERR_BUSY => Err(Error::Busy),
106            GG_ERR_FATAL => Err(Error::Fatal),
107            GG_ERR_INVALID => Err(Error::Invalid),
108            GG_ERR_UNSUPPORTED => Err(Error::Unsupported),
109            GG_ERR_PARSE => Err(Error::Parse),
110            GG_ERR_RANGE => Err(Error::Range),
111            GG_ERR_NOMEM => Err(Error::Nomem),
112            GG_ERR_NOCONN => Err(Error::Noconn),
113            GG_ERR_NODATA => Err(Error::Nodata),
114            GG_ERR_NOENTRY => Err(Error::Noentry),
115            GG_ERR_CONFIG => Err(Error::Config),
116            GG_ERR_REMOTE => Err(Error::Remote),
117            GG_ERR_EXPECTED => Err(Error::Expected),
118            GG_ERR_TIMEOUT => Err(Error::Timeout),
119            GG_ERR_UNAUTHORIZED => Err(Error::Unauthorized),
120            GG_ERR_CONFLICT => Err(Error::Conflict),
121        }
122    }
123}