Skip to main content

embly/
error.rs

1//! errors
2
3use http;
4use httparse;
5use std::error;
6use std::fmt;
7use std::io;
8use std::io::ErrorKind;
9
10/// Result is the embly custom result type
11pub type Result<T> = std::result::Result<T, Error>;
12
13/// Error is an embly error
14#[derive(Debug)]
15pub enum Error {
16    /// Bytes sent to this function were not a valid http request
17    InvalidHttpRequest,
18    /// Wrapper around http::Error
19    Http(http::Error),
20    /// Wrapper around io::Error
21    Io(io::Error),
22    /// Wrapper around httparse::Error
23    HttpParse(httparse::Error),
24}
25
26impl fmt::Display for Error {
27    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28        match *self {
29            // todo: better error message
30            Self::InvalidHttpRequest => write!(f, "The http request is invalid"),
31            Self::Http(ref e) => e.fmt(f),
32            Self::Io(ref e) => e.fmt(f),
33            Self::HttpParse(ref e) => e.fmt(f),
34        }
35    }
36}
37
38impl error::Error for Error {
39    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
40        match *self {
41            Self::InvalidHttpRequest => None,
42            Self::Http(ref e) => Some(e),
43            Self::Io(ref e) => Some(e),
44            Self::HttpParse(ref e) => Some(e),
45        }
46    }
47}
48
49impl From<io::Error> for Error {
50    fn from(err: io::Error) -> Self {
51        Self::Io(err)
52    }
53}
54
55impl From<http::Error> for Error {
56    fn from(err: http::Error) -> Self {
57        Self::Http(err)
58    }
59}
60
61impl From<httparse::Error> for Error {
62    fn from(err: httparse::Error) -> Self {
63        Self::HttpParse(err)
64    }
65}
66
67/// convert a u16 wasi error type to an io::Error
68pub fn wasi_err_to_io_err(err: u16) -> std::io::Result<()> {
69    if err == 0 {
70        Ok(())
71    } else {
72        Err(std::io::Error::from(match err {
73            __WASI_ENOENT => ErrorKind::NotFound,
74            __WASI_EACCES => ErrorKind::PermissionDenied,
75            __WASI_ECONNREFUSED => ErrorKind::ConnectionRefused,
76            __WASI_ECONNRESET => ErrorKind::ConnectionReset,
77            __WASI_ECONNABORTED => ErrorKind::ConnectionAborted,
78            __WASI_ENOTCONN => ErrorKind::NotConnected,
79            __WASI_EADDRINUSE => ErrorKind::AddrInUse,
80            __WASI_EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
81            __WASI_EPIPE => ErrorKind::BrokenPipe,
82            __WASI_EEXIST => ErrorKind::AlreadyExists,
83            __WASI_EAGAIN => ErrorKind::WouldBlock,
84            __WASI_EINVAL => ErrorKind::InvalidInput,
85            __WASI_ETIMEDOUT => ErrorKind::TimedOut,
86            __WASI_EINTR => ErrorKind::Interrupted,
87            __WASI_EIO => ErrorKind::Other,
88            _ => ErrorKind::Other,
89        }))
90    }
91}
92
93/// No error occurred. System call completed successfully.
94pub const __WASI_ESUCCESS: u16 = 0;
95
96/// Argument list too long.
97pub const __WASI_E2BIG: u16 = 1;
98
99/// Permission denied.
100pub const __WASI_EACCES: u16 = 2;
101
102/// Address in use.
103pub const __WASI_EADDRINUSE: u16 = 3;
104
105/// Address not available.
106pub const __WASI_EADDRNOTAVAIL: u16 = 4;
107
108/// Address family not supported.
109pub const __WASI_EAFNOSUPPORT: u16 = 5;
110
111/// Resource unavailable, or operation would block.
112pub const __WASI_EAGAIN: u16 = 6;
113
114/// Connection already in progress.
115pub const __WASI_EALREADY: u16 = 7;
116
117/// Bad file descriptor.
118pub const __WASI_EBADF: u16 = 8;
119
120/// Bad message.
121pub const __WASI_EBADMSG: u16 = 9;
122
123/// Device or resource busy.
124pub const __WASI_EBUSY: u16 = 10;
125
126/// Operation canceled.
127pub const __WASI_ECANCELED: u16 = 11;
128
129/// No child processes.
130pub const __WASI_ECHILD: u16 = 12;
131
132/// Connection aborted.
133pub const __WASI_ECONNABORTED: u16 = 13;
134
135/// Connection refused.
136pub const __WASI_ECONNREFUSED: u16 = 14;
137
138/// Connection reset.
139pub const __WASI_ECONNRESET: u16 = 15;
140
141/// Resource deadlock would occur.
142pub const __WASI_EDEADLK: u16 = 16;
143
144/// Destination address required.
145pub const __WASI_EDESTADDRREQ: u16 = 17;
146
147/// Mathematics argument out of domain of function.
148pub const __WASI_EDOM: u16 = 18;
149
150/// Reserved. (Quota exceeded.)
151pub const __WASI_EDQUOT: u16 = 19;
152
153/// File exists.
154pub const __WASI_EEXIST: u16 = 20;
155
156/// Bad address.
157pub const __WASI_EFAULT: u16 = 21;
158
159/// File too large.
160pub const __WASI_EFBIG: u16 = 22;
161
162/// Host is unreachable.
163pub const __WASI_EHOSTUNREACH: u16 = 23;
164
165/// Identifier removed.
166pub const __WASI_EIDRM: u16 = 24;
167
168/// Illegal byte sequence.
169pub const __WASI_EILSEQ: u16 = 25;
170
171/// Operation in progress.
172pub const __WASI_EINPROGRESS: u16 = 26;
173
174/// Interrupted function.
175pub const __WASI_EINTR: u16 = 27;
176
177/// Invalid argument.
178pub const __WASI_EINVAL: u16 = 28;
179
180/// I/O error.
181pub const __WASI_EIO: u16 = 29;
182
183/// Socket is connected.
184pub const __WASI_EISCONN: u16 = 30;
185
186/// Is a directory.
187pub const __WASI_EISDIR: u16 = 31;
188
189/// Too many levels of symbolic links.
190pub const __WASI_ELOOP: u16 = 32;
191
192/// File descriptor value too large.
193pub const __WASI_EMFILE: u16 = 33;
194
195/// Too many links.
196pub const __WASI_EMLINK: u16 = 34;
197
198/// Message too large.
199pub const __WASI_EMSGSIZE: u16 = 35;
200
201/// Reserved. (Multihop attempted.)
202pub const __WASI_EMULTIHOP: u16 = 36;
203
204/// Filename too long.
205pub const __WASI_ENAMETOOLONG: u16 = 37;
206
207/// Network is down.
208pub const __WASI_ENETDOWN: u16 = 38;
209
210/// Connection aborted by network.
211pub const __WASI_ENETRESET: u16 = 39;
212
213/// Network unreachable.
214pub const __WASI_ENETUNREACH: u16 = 40;
215
216/// Too many files open in system.
217pub const __WASI_ENFILE: u16 = 41;
218
219/// No buffer space available.
220pub const __WASI_ENOBUFS: u16 = 42;
221
222/// No such device.
223pub const __WASI_ENODEV: u16 = 43;
224
225/// No such file or directory.
226pub const __WASI_ENOENT: u16 = 44;
227
228/// Executable file format error.
229pub const __WASI_ENOEXEC: u16 = 45;
230
231/// No locks available.
232pub const __WASI_ENOLCK: u16 = 46;
233
234/// Reserved. (Link has been severed.)
235pub const __WASI_ENOLINK: u16 = 47;
236
237/// Not enough space.
238pub const __WASI_ENOMEM: u16 = 48;
239
240/// No message of the desired type.
241pub const __WASI_ENOMSG: u16 = 49;
242
243/// Protocol not available.
244pub const __WASI_ENOPROTOOPT: u16 = 50;
245
246/// No space left on device.
247pub const __WASI_ENOSPC: u16 = 51;
248
249/// Function not supported. (Always unsupported.)
250pub const __WASI_ENOSYS: u16 = 52;
251
252/// The socket is not connected.
253pub const __WASI_ENOTCONN: u16 = 53;
254
255/// Not a directory or a symbolic link to a directory.
256pub const __WASI_ENOTDIR: u16 = 54;
257
258/// Directory not empty.
259pub const __WASI_ENOTEMPTY: u16 = 55;
260
261/// State not recoverable.
262pub const __WASI_ENOTRECOVERABLE: u16 = 56;
263
264/// Not a socket.
265pub const __WASI_ENOTSOCK: u16 = 57;
266
267/// Not supported, or operation not supported on socket. (Transient unsupported.)
268pub const __WASI_ENOTSUP: u16 = 58;
269
270/// Inappropriate I/O control operation.
271pub const __WASI_ENOTTY: u16 = 59;
272
273/// No such device or address.
274pub const __WASI_ENXIO: u16 = 60;
275
276/// Value too large to be stored in data type.
277pub const __WASI_EOVERFLOW: u16 = 61;
278
279/// Previous owner died.
280pub const __WASI_EOWNERDEAD: u16 = 62;
281
282/// Operation not permitted.
283pub const __WASI_EPERM: u16 = 63;
284
285/// Broken pipe.
286pub const __WASI_EPIPE: u16 = 64;
287
288/// Protocol error.
289pub const __WASI_EPROTO: u16 = 65;
290
291/// Protocol not supported.
292pub const __WASI_EPROTONOSUPPORT: u16 = 66;
293
294/// Protocol wrong type for socket.
295pub const __WASI_EPROTOTYPE: u16 = 67;
296
297/// Result too large.
298pub const __WASI_ERANGE: u16 = 68;
299
300/// Read-only file system.
301pub const __WASI_EROFS: u16 = 69;
302
303/// Invalid seek.
304pub const __WASI_ESPIPE: u16 = 70;
305
306/// No such process.
307pub const __WASI_ESRCH: u16 = 71;
308
309/// Reserved. (Stale file handle.)
310pub const __WASI_ESTALE: u16 = 72;
311
312/// Connection timed out.
313pub const __WASI_ETIMEDOUT: u16 = 73;
314
315/// Text file busy.
316pub const __WASI_ETXTBSY: u16 = 74;
317
318/// Cross-device link.
319pub const __WASI_EXDEV: u16 = 75;
320
321/// Extension: Capabilities insufficient.
322pub const __WASI_ENOTCAPABLE: u16 = 76;