Skip to content

Commit

Permalink
chore(fetcher-service): pass status code to error response
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-wright committed Apr 25, 2024
1 parent f6c542a commit dfbae9e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/sdk/src/http_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand Down
10 changes: 5 additions & 5 deletions services/fetcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand All @@ -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
Expand All @@ -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");
};

Expand Down
20 changes: 11 additions & 9 deletions services/js-poc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand All @@ -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"));
}
},
Expand All @@ -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);
},
};
Expand All @@ -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");
},
};
Expand All @@ -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");
},
};
Expand All @@ -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");
},
};
Expand Down

0 comments on commit dfbae9e

Please sign in to comment.