hyperlight_guest/
error.rs

1/*
2Copyright 2024 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
17use alloc::format;
18use alloc::string::String;
19
20use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
21use {anyhow, serde_json};
22
23pub type Result<T> = core::result::Result<T, HyperlightGuestError>;
24
25#[derive(Debug)]
26pub struct HyperlightGuestError {
27    pub kind: ErrorCode,
28    pub message: String,
29}
30
31impl HyperlightGuestError {
32    pub fn new(kind: ErrorCode, message: String) -> Self {
33        Self { kind, message }
34    }
35}
36
37impl From<anyhow::Error> for HyperlightGuestError {
38    fn from(error: anyhow::Error) -> Self {
39        Self {
40            kind: ErrorCode::GuestError,
41            message: format!("Error: {:?}", error),
42        }
43    }
44}
45
46impl From<serde_json::Error> for HyperlightGuestError {
47    fn from(error: serde_json::Error) -> Self {
48        Self {
49            kind: ErrorCode::GuestError,
50            message: format!("Error: {:?}", error),
51        }
52    }
53}