feat: add server error types and reqwest fetcher
This commit is contained in:
Generated
+2776
-6
File diff suppressed because it is too large
Load Diff
@@ -5,7 +5,12 @@ edition.workspace = true
|
|||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
async-trait = "0.1"
|
||||||
|
reqwest = "0.13"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
thiserror = "2"
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
wasmtime = { version = "45", features = ["component-model"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
|
|||||||
@@ -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 },
|
||||||
|
}
|
||||||
@@ -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 +1,2 @@
|
|||||||
|
pub mod error;
|
||||||
pub mod model;
|
pub mod model;
|
||||||
|
|||||||
Reference in New Issue
Block a user