Skip to main content

hyperlight_common/flatbuffer_wrappers/
guest_error.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025 The Hyperlight Authors.
3
4extern crate flatbuffers;
5
6use alloc::string::{String, ToString};
7
8#[cfg(feature = "tracing")]
9use tracing::{Span, instrument};
10
11use crate::flatbuffers::hyperlight::generated::ErrorCode as FbErrorCode;
12
13#[derive(Debug, Clone, Copy, Eq, PartialEq)]
14#[repr(C)]
15/// `ErrorCode` represents an error that occurred in the Hyperlight Guest.
16pub enum ErrorCode {
17    NoError = 0,
18    UnsupportedParameterType = 2,
19    GuestFunctionNameNotProvided = 3,
20    GuestFunctionNotFound = 4,
21    GuestFunctionIncorrecNoOfParameters = 5,
22    GispatchFunctionPointerNotSet = 6,
23    OutbError = 7,
24    UnknownError = 8,
25    GsCheckFailed = 10,
26    TooManyGuestFunctions = 11,
27    FailureInDlmalloc = 12,
28    MallocFailed = 13,
29    GuestFunctionParameterTypeMismatch = 14,
30    GuestError = 15,
31    ArrayLengthParamIsMissing = 16,
32    HostFunctionError = 17,
33}
34
35impl From<ErrorCode> for FbErrorCode {
36    fn from(error_code: ErrorCode) -> Self {
37        match error_code {
38            ErrorCode::NoError => Self::NoError,
39            ErrorCode::UnsupportedParameterType => Self::UnsupportedParameterType,
40            ErrorCode::GuestFunctionNameNotProvided => Self::GuestFunctionNameNotProvided,
41            ErrorCode::GuestFunctionNotFound => Self::GuestFunctionNotFound,
42            ErrorCode::GuestFunctionIncorrecNoOfParameters => {
43                Self::GuestFunctionIncorrecNoOfParameters
44            }
45            ErrorCode::GispatchFunctionPointerNotSet => Self::GispatchFunctionPointerNotSet,
46            ErrorCode::OutbError => Self::OutbError,
47            ErrorCode::UnknownError => Self::UnknownError,
48            ErrorCode::GsCheckFailed => Self::GsCheckFailed,
49            ErrorCode::TooManyGuestFunctions => Self::TooManyGuestFunctions,
50            ErrorCode::FailureInDlmalloc => Self::FailureInDlmalloc,
51            ErrorCode::MallocFailed => Self::MallocFailed,
52            ErrorCode::GuestFunctionParameterTypeMismatch => {
53                Self::GuestFunctionParameterTypeMismatch
54            }
55            ErrorCode::GuestError => Self::GuestError,
56            ErrorCode::ArrayLengthParamIsMissing => Self::ArrayLengthParamIsMissing,
57            ErrorCode::HostFunctionError => Self::HostError,
58        }
59    }
60}
61
62impl From<FbErrorCode> for ErrorCode {
63    fn from(error_code: FbErrorCode) -> Self {
64        match error_code {
65            FbErrorCode::NoError => Self::NoError,
66            FbErrorCode::UnsupportedParameterType => Self::UnsupportedParameterType,
67            FbErrorCode::GuestFunctionNameNotProvided => Self::GuestFunctionNameNotProvided,
68            FbErrorCode::GuestFunctionNotFound => Self::GuestFunctionNotFound,
69            FbErrorCode::GuestFunctionIncorrecNoOfParameters => {
70                Self::GuestFunctionIncorrecNoOfParameters
71            }
72            FbErrorCode::GispatchFunctionPointerNotSet => Self::GispatchFunctionPointerNotSet,
73            FbErrorCode::OutbError => Self::OutbError,
74            FbErrorCode::GsCheckFailed => Self::GsCheckFailed,
75            FbErrorCode::TooManyGuestFunctions => Self::TooManyGuestFunctions,
76            FbErrorCode::FailureInDlmalloc => Self::FailureInDlmalloc,
77            FbErrorCode::MallocFailed => Self::MallocFailed,
78            FbErrorCode::GuestFunctionParameterTypeMismatch => {
79                Self::GuestFunctionParameterTypeMismatch
80            }
81            FbErrorCode::GuestError => Self::GuestError,
82            FbErrorCode::ArrayLengthParamIsMissing => Self::ArrayLengthParamIsMissing,
83            FbErrorCode::HostError => Self::HostFunctionError,
84            _ => Self::UnknownError,
85        }
86    }
87}
88
89impl From<u64> for ErrorCode {
90    fn from(error_code: u64) -> Self {
91        match error_code {
92            0 => Self::NoError,
93            2 => Self::UnsupportedParameterType,
94            3 => Self::GuestFunctionNameNotProvided,
95            4 => Self::GuestFunctionNotFound,
96            5 => Self::GuestFunctionIncorrecNoOfParameters,
97            6 => Self::GispatchFunctionPointerNotSet,
98            7 => Self::OutbError,
99            8 => Self::UnknownError,
100            10 => Self::GsCheckFailed,
101            11 => Self::TooManyGuestFunctions,
102            12 => Self::FailureInDlmalloc,
103            13 => Self::MallocFailed,
104            14 => Self::GuestFunctionParameterTypeMismatch,
105            15 => Self::GuestError,
106            16 => Self::ArrayLengthParamIsMissing,
107            17 => Self::HostFunctionError,
108            _ => Self::UnknownError,
109        }
110    }
111}
112
113impl From<ErrorCode> for u64 {
114    fn from(error_code: ErrorCode) -> Self {
115        match error_code {
116            ErrorCode::NoError => 0,
117            ErrorCode::UnsupportedParameterType => 2,
118            ErrorCode::GuestFunctionNameNotProvided => 3,
119            ErrorCode::GuestFunctionNotFound => 4,
120            ErrorCode::GuestFunctionIncorrecNoOfParameters => 5,
121            ErrorCode::GispatchFunctionPointerNotSet => 6,
122            ErrorCode::OutbError => 7,
123            ErrorCode::UnknownError => 8,
124            ErrorCode::GsCheckFailed => 10,
125            ErrorCode::TooManyGuestFunctions => 11,
126            ErrorCode::FailureInDlmalloc => 12,
127            ErrorCode::MallocFailed => 13,
128            ErrorCode::GuestFunctionParameterTypeMismatch => 14,
129            ErrorCode::GuestError => 15,
130            ErrorCode::ArrayLengthParamIsMissing => 16,
131            ErrorCode::HostFunctionError => 17,
132        }
133    }
134}
135
136impl From<ErrorCode> for String {
137    fn from(error_code: ErrorCode) -> Self {
138        match error_code {
139            ErrorCode::NoError => "NoError".to_string(),
140            ErrorCode::UnsupportedParameterType => "UnsupportedParameterType".to_string(),
141            ErrorCode::GuestFunctionNameNotProvided => "GuestFunctionNameNotProvided".to_string(),
142            ErrorCode::GuestFunctionNotFound => "GuestFunctionNotFound".to_string(),
143            ErrorCode::GuestFunctionIncorrecNoOfParameters => {
144                "GuestFunctionIncorrecNoOfParameters".to_string()
145            }
146            ErrorCode::GispatchFunctionPointerNotSet => "GispatchFunctionPointerNotSet".to_string(),
147            ErrorCode::OutbError => "OutbError".to_string(),
148            ErrorCode::UnknownError => "UnknownError".to_string(),
149            ErrorCode::GsCheckFailed => "GsCheckFailed".to_string(),
150            ErrorCode::TooManyGuestFunctions => "TooManyGuestFunctions".to_string(),
151            ErrorCode::FailureInDlmalloc => "FailureInDlmalloc".to_string(),
152            ErrorCode::MallocFailed => "MallocFailed".to_string(),
153            ErrorCode::GuestFunctionParameterTypeMismatch => {
154                "GuestFunctionParameterTypeMismatch".to_string()
155            }
156            ErrorCode::GuestError => "GuestError".to_string(),
157            ErrorCode::ArrayLengthParamIsMissing => "ArrayLengthParamIsMissing".to_string(),
158            ErrorCode::HostFunctionError => "HostFunctionError".to_string(),
159        }
160    }
161}
162
163/// `GuestError` represents an error that occurred in the Hyperlight Guest.
164#[derive(Debug, Clone)]
165pub struct GuestError {
166    /// The error code.
167    pub code: ErrorCode,
168    /// The error message.
169    pub message: String,
170}
171
172impl GuestError {
173    #[cfg_attr(feature = "tracing", instrument(skip_all, parent = Span::current(), level= "Trace"))]
174    pub fn new(code: ErrorCode, message: String) -> Self {
175        Self { code, message }
176    }
177}
178
179impl Default for GuestError {
180    #[cfg_attr(feature = "tracing", instrument(parent = Span::current(), level= "Trace"))]
181    fn default() -> Self {
182        Self {
183            code: ErrorCode::NoError,
184            message: String::new(),
185        }
186    }
187}