hyperlight_common/flatbuffer_wrappers/
guest_error.rs

1/*
2Copyright 2025  The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17extern crate flatbuffers;
18
19use alloc::string::{String, ToString};
20
21#[cfg(feature = "tracing")]
22use tracing::{Span, instrument};
23
24use crate::flatbuffers::hyperlight::generated::ErrorCode as FbErrorCode;
25
26#[derive(Debug, Clone, Copy, Eq, PartialEq)]
27#[repr(C)]
28/// `ErrorCode` represents an error that occurred in the Hyperlight Guest.
29pub enum ErrorCode {
30    NoError = 0,
31    UnsupportedParameterType = 2,
32    GuestFunctionNameNotProvided = 3,
33    GuestFunctionNotFound = 4,
34    GuestFunctionIncorrecNoOfParameters = 5,
35    GispatchFunctionPointerNotSet = 6,
36    OutbError = 7,
37    UnknownError = 8,
38    StackOverflow = 9,
39    GsCheckFailed = 10,
40    TooManyGuestFunctions = 11,
41    FailureInDlmalloc = 12,
42    MallocFailed = 13,
43    GuestFunctionParameterTypeMismatch = 14,
44    GuestError = 15,
45    ArrayLengthParamIsMissing = 16,
46    HostFunctionError = 17,
47}
48
49impl From<ErrorCode> for FbErrorCode {
50    fn from(error_code: ErrorCode) -> Self {
51        match error_code {
52            ErrorCode::NoError => Self::NoError,
53            ErrorCode::UnsupportedParameterType => Self::UnsupportedParameterType,
54            ErrorCode::GuestFunctionNameNotProvided => Self::GuestFunctionNameNotProvided,
55            ErrorCode::GuestFunctionNotFound => Self::GuestFunctionNotFound,
56            ErrorCode::GuestFunctionIncorrecNoOfParameters => {
57                Self::GuestFunctionIncorrecNoOfParameters
58            }
59            ErrorCode::GispatchFunctionPointerNotSet => Self::GispatchFunctionPointerNotSet,
60            ErrorCode::OutbError => Self::OutbError,
61            ErrorCode::UnknownError => Self::UnknownError,
62            ErrorCode::StackOverflow => Self::StackOverflow,
63            ErrorCode::GsCheckFailed => Self::GsCheckFailed,
64            ErrorCode::TooManyGuestFunctions => Self::TooManyGuestFunctions,
65            ErrorCode::FailureInDlmalloc => Self::FailureInDlmalloc,
66            ErrorCode::MallocFailed => Self::MallocFailed,
67            ErrorCode::GuestFunctionParameterTypeMismatch => {
68                Self::GuestFunctionParameterTypeMismatch
69            }
70            ErrorCode::GuestError => Self::GuestError,
71            ErrorCode::ArrayLengthParamIsMissing => Self::ArrayLengthParamIsMissing,
72            ErrorCode::HostFunctionError => Self::HostError,
73        }
74    }
75}
76
77impl From<FbErrorCode> for ErrorCode {
78    fn from(error_code: FbErrorCode) -> Self {
79        match error_code {
80            FbErrorCode::NoError => Self::NoError,
81            FbErrorCode::UnsupportedParameterType => Self::UnsupportedParameterType,
82            FbErrorCode::GuestFunctionNameNotProvided => Self::GuestFunctionNameNotProvided,
83            FbErrorCode::GuestFunctionNotFound => Self::GuestFunctionNotFound,
84            FbErrorCode::GuestFunctionIncorrecNoOfParameters => {
85                Self::GuestFunctionIncorrecNoOfParameters
86            }
87            FbErrorCode::GispatchFunctionPointerNotSet => Self::GispatchFunctionPointerNotSet,
88            FbErrorCode::OutbError => Self::OutbError,
89            FbErrorCode::StackOverflow => Self::StackOverflow,
90            FbErrorCode::GsCheckFailed => Self::GsCheckFailed,
91            FbErrorCode::TooManyGuestFunctions => Self::TooManyGuestFunctions,
92            FbErrorCode::FailureInDlmalloc => Self::FailureInDlmalloc,
93            FbErrorCode::MallocFailed => Self::MallocFailed,
94            FbErrorCode::GuestFunctionParameterTypeMismatch => {
95                Self::GuestFunctionParameterTypeMismatch
96            }
97            FbErrorCode::GuestError => Self::GuestError,
98            FbErrorCode::ArrayLengthParamIsMissing => Self::ArrayLengthParamIsMissing,
99            FbErrorCode::HostError => Self::HostFunctionError,
100            _ => Self::UnknownError,
101        }
102    }
103}
104
105impl From<u64> for ErrorCode {
106    fn from(error_code: u64) -> Self {
107        match error_code {
108            0 => Self::NoError,
109            2 => Self::UnsupportedParameterType,
110            3 => Self::GuestFunctionNameNotProvided,
111            4 => Self::GuestFunctionNotFound,
112            5 => Self::GuestFunctionIncorrecNoOfParameters,
113            6 => Self::GispatchFunctionPointerNotSet,
114            7 => Self::OutbError,
115            8 => Self::UnknownError,
116            9 => Self::StackOverflow,
117            10 => Self::GsCheckFailed,
118            11 => Self::TooManyGuestFunctions,
119            12 => Self::FailureInDlmalloc,
120            13 => Self::MallocFailed,
121            14 => Self::GuestFunctionParameterTypeMismatch,
122            15 => Self::GuestError,
123            16 => Self::ArrayLengthParamIsMissing,
124            17 => Self::HostFunctionError,
125            _ => Self::UnknownError,
126        }
127    }
128}
129
130impl From<ErrorCode> for u64 {
131    fn from(error_code: ErrorCode) -> Self {
132        match error_code {
133            ErrorCode::NoError => 0,
134            ErrorCode::UnsupportedParameterType => 2,
135            ErrorCode::GuestFunctionNameNotProvided => 3,
136            ErrorCode::GuestFunctionNotFound => 4,
137            ErrorCode::GuestFunctionIncorrecNoOfParameters => 5,
138            ErrorCode::GispatchFunctionPointerNotSet => 6,
139            ErrorCode::OutbError => 7,
140            ErrorCode::UnknownError => 8,
141            ErrorCode::StackOverflow => 9,
142            ErrorCode::GsCheckFailed => 10,
143            ErrorCode::TooManyGuestFunctions => 11,
144            ErrorCode::FailureInDlmalloc => 12,
145            ErrorCode::MallocFailed => 13,
146            ErrorCode::GuestFunctionParameterTypeMismatch => 14,
147            ErrorCode::GuestError => 15,
148            ErrorCode::ArrayLengthParamIsMissing => 16,
149            ErrorCode::HostFunctionError => 17,
150        }
151    }
152}
153
154impl From<ErrorCode> for String {
155    fn from(error_code: ErrorCode) -> Self {
156        match error_code {
157            ErrorCode::NoError => "NoError".to_string(),
158            ErrorCode::UnsupportedParameterType => "UnsupportedParameterType".to_string(),
159            ErrorCode::GuestFunctionNameNotProvided => "GuestFunctionNameNotProvided".to_string(),
160            ErrorCode::GuestFunctionNotFound => "GuestFunctionNotFound".to_string(),
161            ErrorCode::GuestFunctionIncorrecNoOfParameters => {
162                "GuestFunctionIncorrecNoOfParameters".to_string()
163            }
164            ErrorCode::GispatchFunctionPointerNotSet => "GispatchFunctionPointerNotSet".to_string(),
165            ErrorCode::OutbError => "OutbError".to_string(),
166            ErrorCode::UnknownError => "UnknownError".to_string(),
167            ErrorCode::StackOverflow => "StackOverflow".to_string(),
168            ErrorCode::GsCheckFailed => "GsCheckFailed".to_string(),
169            ErrorCode::TooManyGuestFunctions => "TooManyGuestFunctions".to_string(),
170            ErrorCode::FailureInDlmalloc => "FailureInDlmalloc".to_string(),
171            ErrorCode::MallocFailed => "MallocFailed".to_string(),
172            ErrorCode::GuestFunctionParameterTypeMismatch => {
173                "GuestFunctionParameterTypeMismatch".to_string()
174            }
175            ErrorCode::GuestError => "GuestError".to_string(),
176            ErrorCode::ArrayLengthParamIsMissing => "ArrayLengthParamIsMissing".to_string(),
177            ErrorCode::HostFunctionError => "HostFunctionError".to_string(),
178        }
179    }
180}
181
182/// `GuestError` represents an error that occurred in the Hyperlight Guest.
183#[derive(Debug, Clone)]
184pub struct GuestError {
185    /// The error code.
186    pub code: ErrorCode,
187    /// The error message.
188    pub message: String,
189}
190
191impl GuestError {
192    #[cfg_attr(feature = "tracing", instrument(skip_all, parent = Span::current(), level= "Trace"))]
193    pub fn new(code: ErrorCode, message: String) -> Self {
194        Self { code, message }
195    }
196}
197
198impl Default for GuestError {
199    #[cfg_attr(feature = "tracing", instrument(parent = Span::current(), level= "Trace"))]
200    fn default() -> Self {
201        Self {
202            code: ErrorCode::NoError,
203            message: String::new(),
204        }
205    }
206}