Skip to main content

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    GsCheckFailed = 10,
39    TooManyGuestFunctions = 11,
40    FailureInDlmalloc = 12,
41    MallocFailed = 13,
42    GuestFunctionParameterTypeMismatch = 14,
43    GuestError = 15,
44    ArrayLengthParamIsMissing = 16,
45    HostFunctionError = 17,
46}
47
48impl From<ErrorCode> for FbErrorCode {
49    fn from(error_code: ErrorCode) -> Self {
50        match error_code {
51            ErrorCode::NoError => Self::NoError,
52            ErrorCode::UnsupportedParameterType => Self::UnsupportedParameterType,
53            ErrorCode::GuestFunctionNameNotProvided => Self::GuestFunctionNameNotProvided,
54            ErrorCode::GuestFunctionNotFound => Self::GuestFunctionNotFound,
55            ErrorCode::GuestFunctionIncorrecNoOfParameters => {
56                Self::GuestFunctionIncorrecNoOfParameters
57            }
58            ErrorCode::GispatchFunctionPointerNotSet => Self::GispatchFunctionPointerNotSet,
59            ErrorCode::OutbError => Self::OutbError,
60            ErrorCode::UnknownError => Self::UnknownError,
61            ErrorCode::GsCheckFailed => Self::GsCheckFailed,
62            ErrorCode::TooManyGuestFunctions => Self::TooManyGuestFunctions,
63            ErrorCode::FailureInDlmalloc => Self::FailureInDlmalloc,
64            ErrorCode::MallocFailed => Self::MallocFailed,
65            ErrorCode::GuestFunctionParameterTypeMismatch => {
66                Self::GuestFunctionParameterTypeMismatch
67            }
68            ErrorCode::GuestError => Self::GuestError,
69            ErrorCode::ArrayLengthParamIsMissing => Self::ArrayLengthParamIsMissing,
70            ErrorCode::HostFunctionError => Self::HostError,
71        }
72    }
73}
74
75impl From<FbErrorCode> for ErrorCode {
76    fn from(error_code: FbErrorCode) -> Self {
77        match error_code {
78            FbErrorCode::NoError => Self::NoError,
79            FbErrorCode::UnsupportedParameterType => Self::UnsupportedParameterType,
80            FbErrorCode::GuestFunctionNameNotProvided => Self::GuestFunctionNameNotProvided,
81            FbErrorCode::GuestFunctionNotFound => Self::GuestFunctionNotFound,
82            FbErrorCode::GuestFunctionIncorrecNoOfParameters => {
83                Self::GuestFunctionIncorrecNoOfParameters
84            }
85            FbErrorCode::GispatchFunctionPointerNotSet => Self::GispatchFunctionPointerNotSet,
86            FbErrorCode::OutbError => Self::OutbError,
87            FbErrorCode::GsCheckFailed => Self::GsCheckFailed,
88            FbErrorCode::TooManyGuestFunctions => Self::TooManyGuestFunctions,
89            FbErrorCode::FailureInDlmalloc => Self::FailureInDlmalloc,
90            FbErrorCode::MallocFailed => Self::MallocFailed,
91            FbErrorCode::GuestFunctionParameterTypeMismatch => {
92                Self::GuestFunctionParameterTypeMismatch
93            }
94            FbErrorCode::GuestError => Self::GuestError,
95            FbErrorCode::ArrayLengthParamIsMissing => Self::ArrayLengthParamIsMissing,
96            FbErrorCode::HostError => Self::HostFunctionError,
97            _ => Self::UnknownError,
98        }
99    }
100}
101
102impl From<u64> for ErrorCode {
103    fn from(error_code: u64) -> Self {
104        match error_code {
105            0 => Self::NoError,
106            2 => Self::UnsupportedParameterType,
107            3 => Self::GuestFunctionNameNotProvided,
108            4 => Self::GuestFunctionNotFound,
109            5 => Self::GuestFunctionIncorrecNoOfParameters,
110            6 => Self::GispatchFunctionPointerNotSet,
111            7 => Self::OutbError,
112            8 => Self::UnknownError,
113            10 => Self::GsCheckFailed,
114            11 => Self::TooManyGuestFunctions,
115            12 => Self::FailureInDlmalloc,
116            13 => Self::MallocFailed,
117            14 => Self::GuestFunctionParameterTypeMismatch,
118            15 => Self::GuestError,
119            16 => Self::ArrayLengthParamIsMissing,
120            17 => Self::HostFunctionError,
121            _ => Self::UnknownError,
122        }
123    }
124}
125
126impl From<ErrorCode> for u64 {
127    fn from(error_code: ErrorCode) -> Self {
128        match error_code {
129            ErrorCode::NoError => 0,
130            ErrorCode::UnsupportedParameterType => 2,
131            ErrorCode::GuestFunctionNameNotProvided => 3,
132            ErrorCode::GuestFunctionNotFound => 4,
133            ErrorCode::GuestFunctionIncorrecNoOfParameters => 5,
134            ErrorCode::GispatchFunctionPointerNotSet => 6,
135            ErrorCode::OutbError => 7,
136            ErrorCode::UnknownError => 8,
137            ErrorCode::GsCheckFailed => 10,
138            ErrorCode::TooManyGuestFunctions => 11,
139            ErrorCode::FailureInDlmalloc => 12,
140            ErrorCode::MallocFailed => 13,
141            ErrorCode::GuestFunctionParameterTypeMismatch => 14,
142            ErrorCode::GuestError => 15,
143            ErrorCode::ArrayLengthParamIsMissing => 16,
144            ErrorCode::HostFunctionError => 17,
145        }
146    }
147}
148
149impl From<ErrorCode> for String {
150    fn from(error_code: ErrorCode) -> Self {
151        match error_code {
152            ErrorCode::NoError => "NoError".to_string(),
153            ErrorCode::UnsupportedParameterType => "UnsupportedParameterType".to_string(),
154            ErrorCode::GuestFunctionNameNotProvided => "GuestFunctionNameNotProvided".to_string(),
155            ErrorCode::GuestFunctionNotFound => "GuestFunctionNotFound".to_string(),
156            ErrorCode::GuestFunctionIncorrecNoOfParameters => {
157                "GuestFunctionIncorrecNoOfParameters".to_string()
158            }
159            ErrorCode::GispatchFunctionPointerNotSet => "GispatchFunctionPointerNotSet".to_string(),
160            ErrorCode::OutbError => "OutbError".to_string(),
161            ErrorCode::UnknownError => "UnknownError".to_string(),
162            ErrorCode::GsCheckFailed => "GsCheckFailed".to_string(),
163            ErrorCode::TooManyGuestFunctions => "TooManyGuestFunctions".to_string(),
164            ErrorCode::FailureInDlmalloc => "FailureInDlmalloc".to_string(),
165            ErrorCode::MallocFailed => "MallocFailed".to_string(),
166            ErrorCode::GuestFunctionParameterTypeMismatch => {
167                "GuestFunctionParameterTypeMismatch".to_string()
168            }
169            ErrorCode::GuestError => "GuestError".to_string(),
170            ErrorCode::ArrayLengthParamIsMissing => "ArrayLengthParamIsMissing".to_string(),
171            ErrorCode::HostFunctionError => "HostFunctionError".to_string(),
172        }
173    }
174}
175
176/// `GuestError` represents an error that occurred in the Hyperlight Guest.
177#[derive(Debug, Clone)]
178pub struct GuestError {
179    /// The error code.
180    pub code: ErrorCode,
181    /// The error message.
182    pub message: String,
183}
184
185impl GuestError {
186    #[cfg_attr(feature = "tracing", instrument(skip_all, parent = Span::current(), level= "Trace"))]
187    pub fn new(code: ErrorCode, message: String) -> Self {
188        Self { code, message }
189    }
190}
191
192impl Default for GuestError {
193    #[cfg_attr(feature = "tracing", instrument(parent = Span::current(), level= "Trace"))]
194    fn default() -> Self {
195        Self {
196            code: ErrorCode::NoError,
197            message: String::new(),
198        }
199    }
200}