Browse Source
This commit fixes an issue with errors in the `wasmtime-wasi-http` crate by using the `trappable_error_type` bindgen configuration option in the same manner as other WASI interfaces in the `wasmtime-wasi` crate. Unfortunately due to coherence the `TrappableError<T>` type itself could not be used but it was small enough it wasn't much effort to duplicate. Closes #8269pull/8298/head
Alex Crichton
7 months ago
committed by
GitHub
9 changed files with 127 additions and 37 deletions
@ -0,0 +1,16 @@ |
|||
use test_programs::wasi::http::outgoing_handler::{handle, OutgoingRequest}; |
|||
use test_programs::wasi::http::types::{Fields, Method, Scheme}; |
|||
|
|||
fn main() { |
|||
let fields = Fields::new(); |
|||
let req = OutgoingRequest::new(fields); |
|||
req.set_method(&Method::Get).unwrap(); |
|||
req.set_scheme(Some(&Scheme::Https)).unwrap(); |
|||
req.set_authority(Some("example.com")).unwrap(); |
|||
|
|||
// Don't set path/query
|
|||
// req.set_path_with_query(Some("/")).unwrap();
|
|||
|
|||
let res = handle(req, None); |
|||
assert!(res.is_err()); |
|||
} |
@ -0,0 +1,55 @@ |
|||
use crate::bindings::http::types::ErrorCode; |
|||
use std::error::Error; |
|||
use std::fmt; |
|||
use wasmtime_wasi::ResourceTableError; |
|||
|
|||
pub type HttpResult<T, E = HttpError> = Result<T, E>; |
|||
|
|||
/// A `wasi:http`-specific error type used to represent either a trap or an
|
|||
/// [`ErrorCode`].
|
|||
///
|
|||
/// Modeled after [`TrappableError`](wasmtime_wasi::TrappableError).
|
|||
#[repr(transparent)] |
|||
pub struct HttpError { |
|||
err: anyhow::Error, |
|||
} |
|||
|
|||
impl HttpError { |
|||
pub fn trap(err: impl Into<anyhow::Error>) -> HttpError { |
|||
HttpError { err: err.into() } |
|||
} |
|||
|
|||
pub fn downcast(self) -> anyhow::Result<ErrorCode> { |
|||
self.err.downcast() |
|||
} |
|||
|
|||
pub fn downcast_ref(&self) -> Option<&ErrorCode> { |
|||
self.err.downcast_ref() |
|||
} |
|||
} |
|||
|
|||
impl From<ErrorCode> for HttpError { |
|||
fn from(error: ErrorCode) -> Self { |
|||
Self { err: error.into() } |
|||
} |
|||
} |
|||
|
|||
impl From<ResourceTableError> for HttpError { |
|||
fn from(error: ResourceTableError) -> Self { |
|||
HttpError::trap(error) |
|||
} |
|||
} |
|||
|
|||
impl fmt::Debug for HttpError { |
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|||
self.err.fmt(f) |
|||
} |
|||
} |
|||
|
|||
impl fmt::Display for HttpError { |
|||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|||
self.err.fmt(f) |
|||
} |
|||
} |
|||
|
|||
impl Error for HttpError {} |
Loading…
Reference in new issue