diff --git a/lib/sdk/src/http_util.rs b/lib/sdk/src/http_util.rs index 84829d2ac..278c82878 100644 --- a/lib/sdk/src/http_util.rs +++ b/lib/sdk/src/http_util.rs @@ -6,11 +6,12 @@ use crate::header::{HttpOverrides, HttpResponse}; pub async fn respond_with_error( connection: &mut Connection, error: &[u8], + status_code: u16, is_http: bool, ) -> anyhow::Result<()> { if is_http { let headers = HttpOverrides { - status: Some(400_u16), + status: Some(status_code), headers: None, }; let header_bytes = serde_json::to_vec(&headers).context("Failed to serialize headers")?; diff --git a/services/fetcher/src/lib.rs b/services/fetcher/src/lib.rs index 88517d466..2e9af8285 100644 --- a/services/fetcher/src/lib.rs +++ b/services/fetcher/src/lib.rs @@ -110,19 +110,19 @@ async fn handle_request(conn: &mut Connection, origin: Origin, uri: Bytes) -> an // Fetch the content from the origin let hash = match origin { Origin::Unknown => { - respond_with_error(conn, b"Unknown origin", is_http).await?; + respond_with_error(conn, b"Unknown origin", 400, is_http).await?; bail!("unknown origin"); }, Origin::Blake3 => { if uri.len() != 32 { - respond_with_error(conn, b"Invalid blake3 hash", is_http).await?; + respond_with_error(conn, b"Invalid blake3 hash", 400, is_http).await?; bail!("expected a 32 byte hash"); } // Fetch the content from the network let hash = *array_ref!(uri, 0, 32); if !fn_sdk::api::fetch_blake3(hash).await { - respond_with_error(conn, b"Failed to fetch blake3 content", is_http).await?; + respond_with_error(conn, b"Failed to fetch blake3 content", 400, is_http).await?; bail!("failed to fetch content"); } @@ -131,7 +131,7 @@ async fn handle_request(conn: &mut Connection, origin: Origin, uri: Bytes) -> an origin => { // Fetch the content from the origin let Some(hash) = fn_sdk::api::fetch_from_origin(origin.into(), uri).await else { - respond_with_error(conn, b"Failed to fetch from origin", is_http).await?; + respond_with_error(conn, b"Failed to fetch from origin", 400, is_http).await?; bail!("failed to fetch from origin"); }; hash @@ -142,7 +142,7 @@ async fn handle_request(conn: &mut Connection, origin: Origin, uri: Bytes) -> an // Get the content from the blockstore let Ok(content_handle) = fn_sdk::blockstore::ContentHandle::load(&hash).await else { - // TODO(matthias): why aren't we sending an error to the handshake here? + respond_with_error(conn, b"Internal error", 500, is_http).await?; bail!("failed to load content handle from the blockstore"); }; diff --git a/services/js-poc/src/lib.rs b/services/js-poc/src/lib.rs index 09a5ee4f7..e3e1408b8 100644 --- a/services/js-poc/src/lib.rs +++ b/services/js-poc/src/lib.rs @@ -119,7 +119,7 @@ async fn handle_request( let hash = hex::decode(uri).context("failed to decode blake3 hash")?; if hash.len() != 32 { - respond_with_error(connection, b"Invalid blake3 hash length", is_http).await?; + respond_with_error(connection, b"Invalid blake3 hash length", 400, is_http).await?; return Err(anyhow!("invalid blake3 hash length")); } @@ -128,7 +128,8 @@ async fn handle_request( if fn_sdk::api::fetch_blake3(hash).await { hash } else { - respond_with_error(connection, b"Failed to fetch blake3 content", is_http).await?; + respond_with_error(connection, b"Failed to fetch blake3 content", 400, is_http) + .await?; return Err(anyhow!("failed to fetch file")); } }, @@ -141,14 +142,15 @@ async fn handle_request( { Some(hash) => hash, None => { - respond_with_error(connection, b"Failed to fetch from origin", is_http).await?; + respond_with_error(connection, b"Failed to fetch from origin", 400, is_http) + .await?; return Err(anyhow!("failed to fetch from origin")); }, } }, o => { let err = anyhow!("unknown origin: {o:?}"); - respond_with_error(connection, err.to_string().as_bytes(), is_http).await?; + respond_with_error(connection, err.to_string().as_bytes(), 400, is_http).await?; return Err(err); }, }; @@ -172,7 +174,7 @@ async fn handle_request( let mut runtime = match Runtime::new(location.clone()) { Ok(runtime) => runtime, Err(e) => { - respond_with_error(connection, e.to_string().as_bytes(), is_http).await?; + respond_with_error(connection, e.to_string().as_bytes(), 400, is_http).await?; return Err(e).context("failed to initialize runtime"); }, }; @@ -186,11 +188,11 @@ async fn handle_request( { Ok(Some(res)) => res, Ok(None) => { - respond_with_error(connection, b"no response available", is_http).await?; + respond_with_error(connection, b"no response available", 400, is_http).await?; bail!("no response available"); }, Err(e) => { - respond_with_error(connection, e.to_string().as_bytes(), is_http).await?; + respond_with_error(connection, e.to_string().as_bytes(), 400, is_http).await?; return Err(e).context("failed to run javascript"); }, }; @@ -202,11 +204,11 @@ async fn handle_request( { Ok(Ok(res)) => res, Ok(Err(e)) => { - respond_with_error(connection, e.to_string().as_bytes(), is_http).await?; + respond_with_error(connection, e.to_string().as_bytes(), 400, is_http).await?; return Err(e).context("failed to resolve output"); }, Err(e) => { - respond_with_error(connection, b"Request timeout", is_http).await?; + respond_with_error(connection, b"Request timeout", 400, is_http).await?; return Err(e).context("execution timeout"); }, };