proxy_wasm_experimental/
error.rs1use std::fmt;
16
17use crate::types::Status;
18
19pub type Error = Box<dyn std::error::Error + Send + Sync>;
23
24pub type Result<T> = core::result::Result<T, Error>;
28
29#[derive(Debug)]
31pub struct HostCallError<'a> {
32 function: &'a str,
33 status: Status,
34}
35
36impl<'a> HostCallError<'a> {
37 pub(crate) fn new(function: &'a str, status: Status) -> Self {
38 HostCallError { function, status }
39 }
40
41 pub fn module(&self) -> &'a str {
42 "env"
43 }
44
45 pub fn function(&self) -> &'a str {
46 self.function
47 }
48
49 pub fn status(&self) -> Status {
50 self.status
51 }
52}
53
54impl<'a> fmt::Display for HostCallError<'a> {
55 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56 write!(
57 f,
58 "call to the host ABI function \"{}.{}\" has failed with status code {}",
59 self.module(),
60 self.function,
61 self.status as u32,
62 )
63 }
64}
65
66impl<'a> std::error::Error for HostCallError<'a> {}
67
68#[derive(Debug)]
70pub struct HostResponseError<'a> {
71 function: &'a str,
72 error: Error,
73}
74
75impl<'a> HostResponseError<'a> {
76 pub(crate) fn new(function: &'a str, error: Error) -> Self {
77 HostResponseError { function, error }
78 }
79
80 pub fn module(&self) -> &'a str {
81 "env"
82 }
83
84 pub fn function(&self) -> &'a str {
85 self.function
86 }
87}
88
89impl<'a> fmt::Display for HostResponseError<'a> {
90 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91 write!(
92 f,
93 "failed to parse response from the host ABI function \"{}.{}\": {}",
94 self.module(),
95 self.function,
96 self.error,
97 )
98 }
99}
100
101impl<'a> std::error::Error for HostResponseError<'a> {
102 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
103 Some(&*self.error)
104 }
105}