feat: add server error types and reqwest fetcher

This commit is contained in:
2026-06-05 15:10:11 +02:00
parent 86b196c2d8
commit 9f3ff2633c
5 changed files with 2837 additions and 6 deletions
Generated
+2776 -6
View File
File diff suppressed because it is too large Load Diff
+5
View File
@@ -5,7 +5,12 @@ edition.workspace = true
authors.workspace = true
[dependencies]
async-trait = "0.1"
reqwest = "0.13"
serde = { version = "1", features = ["derive"] }
thiserror = "2"
tokio = { version = "1", features = ["full"] }
wasmtime = { version = "45", features = ["component-model"] }
[dev-dependencies]
serde_json = "1"
+22
View File
@@ -0,0 +1,22 @@
use thiserror::Error;
/// Errors from hosting/calling a WASM component.
#[derive(Debug, Error)]
pub enum HostError {
#[error("wasm error: {0}")]
Wasm(#[from] wasmtime::Error),
#[error("io error: {0}")]
Io(#[from] std::io::Error),
}
#[derive(Debug, Error)]
pub enum FetchError {
#[error("request failed: {0}")]
Request(#[from] reqwest::Error),
}
#[derive(Debug, Error)]
pub enum ConfigError {
#[error("invalid value for {key}: {message}")]
Invalid { key: String, message: String },
}
+33
View File
@@ -0,0 +1,33 @@
use std::time::Duration;
use async_trait::async_trait;
use crate::error::FetchError;
use crate::model::FetchedResponse;
use crate::service::Fetch;
pub struct ReqwestFetcher {
client: reqwest::Client,
}
impl ReqwestFetcher {
pub fn new(timeout: Duration) -> Result<Self, FetchError> {
let client = reqwest::Client::builder()
.timeout(timeout)
.user_agent(concat!("whoareyou/", env!("CARGO_PKG_VERSION")))
.build()?;
Ok(Self { client })
}
}
#[async_trait]
impl Fetch for ReqwestFetcher {
async fn fetch(&self, url: &str) -> Result<FetchedResponse, FetchError> {
let response = self.client.get(url).send().await?;
let status = response.status().as_u16();
let body = response.text().await?;
Ok(FetchedResponse { status, body })
}
}
+1
View File
@@ -1 +1,2 @@
pub mod error;
pub mod model;