|
1 | | -//! `wasi_common::Error` is now `anyhow::Error`. |
| 1 | +//! wasi-common uses an [`Error`] type which represents either a preview 1 [`Errno`] enum, on |
| 2 | +//! [`anyhow::Error`] for trapping execution. |
2 | 3 | //! |
3 | | -//! Snapshots (right now only `wasi_common::snapshots::preview_1`) contains |
4 | | -//! all of the logic for transforming an `Error` into the snapshot's own |
5 | | -//! `Errno`. They may do so by downcasting the error into any of: |
6 | | -//! * `std::io::Error` - these are thrown by `std`, `cap_std`, etc for most of |
7 | | -//! the operations WASI is concerned with. |
8 | | -//! * `wasi_common::ErrorKind` - these are a subset of the Errnos, and are |
9 | | -//! constructed directly by wasi-common or an impl rather than coming from the |
10 | | -//! OS or some library which doesn't know about WASI. |
11 | | -//! * `wiggle::GuestError` |
12 | | -//! * `std::num::TryFromIntError` |
13 | | -//! * `std::str::Utf8Error` |
14 | | -//! and then applying specialized logic to translate each of those into |
15 | | -//! `Errno`s. |
16 | | -//! |
17 | | -//! The `wasi_common::ErrorExt` trait provides human-friendly constructors for |
18 | | -//! the `wasi_common::ErrorKind` variants . |
19 | | -//! |
20 | | -//! If you throw an error that does not downcast to one of those, it will turn |
21 | | -//! into a `wiggle::Trap` and terminate execution. |
22 | | -//! |
23 | | -//! The real value of using `anyhow::Error` here is being able to use |
24 | | -//! `anyhow::Result::context` to aid in debugging of errors. |
| 4 | +//! The user can construct an [`Error`] out of an [`Errno`] using the `From`/`Into` traits. |
| 5 | +//! They may also use [`Error::trap`] to construct an error that traps execution. The contents |
| 6 | +//! can be inspected with [`Error::downcast`] and [`Error::downcast_ref`]. Additional context |
| 7 | +//! can be provided with the [`Error::context`] method. This context is only observable with the |
| 8 | +//! `Display` and `Debug` impls of the error. |
25 | 9 |
|
26 | | -pub use anyhow::{Context, Error}; |
| 10 | +pub use crate::snapshots::preview_1::error::{Errno, Error, ErrorExt}; |
27 | 11 | use std::fmt; |
28 | 12 |
|
29 | | -/// Internal error type for the `wasi-common` crate. |
30 | | -/// |
31 | | -/// This Contains variants of the WASI `$errno` type that are used internally |
32 | | -/// by the crate, and which aren't one-to-one with a `std::io::ErrorKind` |
33 | | -/// error. |
34 | | -/// |
35 | | -/// When the Rust [io_error_more] feature is stabilized, that will enable |
36 | | -/// us to replace several more of these codes with `std::io::ErrorKind` codes. |
37 | | -/// |
38 | | -/// [io_error_more]: https://doc.rust-lang.org/beta/unstable-book/library-features/io-error-more.html |
39 | | -#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)] |
40 | | -#[non_exhaustive] |
41 | | -pub enum ErrorKind { |
42 | | - /// Errno::TooBig: Argument list too long |
43 | | - #[error("TooBig: Argument list too long")] |
44 | | - TooBig, |
45 | | - /// Errno::Badf: Bad file descriptor |
46 | | - #[error("Badf: Bad file descriptor")] |
47 | | - Badf, |
48 | | - /// Errno::Ilseq: Illegal byte sequence |
49 | | - #[error("Ilseq: Illegal byte sequence")] |
50 | | - Ilseq, |
51 | | - /// Errno::Io: I/O error |
52 | | - #[error("Io: I/O error")] |
53 | | - Io, |
54 | | - /// Errno::Nametoolong: Filename too long |
55 | | - #[error("Nametoolong: Filename too long")] |
56 | | - Nametoolong, |
57 | | - /// Errno::Notdir: Not a directory or a symbolic link to a directory. |
58 | | - #[error("Notdir: Not a directory or a symbolic link to a directory")] |
59 | | - Notdir, |
60 | | - /// Errno::Notsup: Not supported, or operation not supported on socket. |
61 | | - #[error("Notsup: Not supported, or operation not supported on socket")] |
62 | | - Notsup, |
63 | | - /// Errno::Overflow: Value too large to be stored in data type. |
64 | | - #[error("Overflow: Value too large to be stored in data type")] |
65 | | - Overflow, |
66 | | - /// Errno::Range: Result too large |
67 | | - #[error("Range: Result too large")] |
68 | | - Range, |
69 | | - /// Errno::Spipe: Invalid seek |
70 | | - #[error("Spipe: Invalid seek")] |
71 | | - Spipe, |
72 | | - /// Errno::Perm: Permission denied |
73 | | - #[error("Permission denied")] |
74 | | - Perm, |
75 | | -} |
76 | | - |
77 | | -pub trait ErrorExt { |
78 | | - fn trap(msg: impl Into<String>) -> Self; |
79 | | - fn not_found() -> Self; |
80 | | - fn too_big() -> Self; |
81 | | - fn badf() -> Self; |
82 | | - fn exist() -> Self; |
83 | | - fn illegal_byte_sequence() -> Self; |
84 | | - fn invalid_argument() -> Self; |
85 | | - fn io() -> Self; |
86 | | - fn name_too_long() -> Self; |
87 | | - fn not_dir() -> Self; |
88 | | - fn not_supported() -> Self; |
89 | | - fn overflow() -> Self; |
90 | | - fn range() -> Self; |
91 | | - fn seek_pipe() -> Self; |
92 | | - fn perm() -> Self; |
93 | | -} |
94 | | - |
95 | | -impl ErrorExt for Error { |
96 | | - fn trap(msg: impl Into<String>) -> Self { |
97 | | - anyhow::anyhow!(msg.into()) |
98 | | - } |
99 | | - fn not_found() -> Self { |
100 | | - std::io::Error::from(std::io::ErrorKind::NotFound).into() |
101 | | - } |
102 | | - fn too_big() -> Self { |
103 | | - ErrorKind::TooBig.into() |
104 | | - } |
105 | | - fn badf() -> Self { |
106 | | - ErrorKind::Badf.into() |
107 | | - } |
108 | | - fn exist() -> Self { |
109 | | - std::io::Error::from(std::io::ErrorKind::AlreadyExists).into() |
110 | | - } |
111 | | - fn illegal_byte_sequence() -> Self { |
112 | | - ErrorKind::Ilseq.into() |
113 | | - } |
114 | | - fn invalid_argument() -> Self { |
115 | | - std::io::Error::from(std::io::ErrorKind::InvalidInput).into() |
116 | | - } |
117 | | - fn io() -> Self { |
118 | | - ErrorKind::Io.into() |
119 | | - } |
120 | | - fn name_too_long() -> Self { |
121 | | - ErrorKind::Nametoolong.into() |
122 | | - } |
123 | | - fn not_dir() -> Self { |
124 | | - ErrorKind::Notdir.into() |
125 | | - } |
126 | | - fn not_supported() -> Self { |
127 | | - ErrorKind::Notsup.into() |
128 | | - } |
129 | | - fn overflow() -> Self { |
130 | | - ErrorKind::Overflow.into() |
131 | | - } |
132 | | - fn range() -> Self { |
133 | | - ErrorKind::Range.into() |
134 | | - } |
135 | | - fn seek_pipe() -> Self { |
136 | | - ErrorKind::Spipe.into() |
137 | | - } |
138 | | - fn perm() -> Self { |
139 | | - ErrorKind::Perm.into() |
140 | | - } |
141 | | -} |
142 | | - |
143 | 13 | /// An error returned from the `proc_exit` host syscall. |
144 | 14 | /// |
145 | 15 | /// Embedders can test if an error returned from wasm is this error, in which |
|
0 commit comments