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>;
#[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 {}