Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
feat: better server-side errors (#9)
Browse files Browse the repository at this point in the history
If a request with an incompatible signature is sent there is no feedback
to the requester about what went wrong. This PR implements one fix (by
returning error `400` and pointing what `k` is expected), but can be
extended in the future.
  • Loading branch information
luizirber authored May 13, 2023
1 parent ed63983 commit 4dca464
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt", "json"] }

[profile.release]
debug = true
debug = 1
#lto = true # Enable link-time optimization
71 changes: 54 additions & 17 deletions crates/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{borrow::Cow, net::SocketAddr, path::PathBuf, sync::Arc, time::Duration};

use axum::{
body::Bytes,
body::{BoxBody, Bytes},
error_handling::HandleErrorLayer,
extract::{ContentLengthLimit, Extension},
http::StatusCode,
response::IntoResponse,
http::{header, StatusCode},
response::{IntoResponse, Response},
routing::{get_service, post},
Router,
};
Expand Down Expand Up @@ -63,6 +63,11 @@ async fn main() {
traces_sample_rate: 1.0,
enable_profiling: true,
profiles_sample_rate: 1.0,
environment: Some(
std::env::var("MASTIFF_ENVIRONMENT")
.unwrap_or("development".into())
.into(),
),
..Default::default()
},
));
Expand Down Expand Up @@ -135,16 +140,18 @@ impl State {
let threshold = self.threshold;
let template = self.template.clone();

let (matches, query_size) = tokio::task::spawn_blocking(move || {
let Ok((matches, query_size)) = tokio::task::spawn_blocking(move || {
if let Some(Sketch::MinHash(mh)) = query.select_sketch(&template) {
let counter = db.counter_for_query(mh);
let matches = db.matches_from_counter(counter, threshold);
(matches, mh.size() as f64)
Ok((matches, mh.size() as f64))
} else {
todo!()
Err("Could not extract compatible sketch to compare")
}
})
.await?;
.await? else {
return Err("Could not extract compatible sketch to compare".into())
};

let mut csv = vec!["SRA accession,containment".into()];
csv.extend(matches.into_iter().map(|(path, size)| {
Expand All @@ -157,22 +164,52 @@ impl State {
}));
Ok(csv)
}

fn parse_sig(&self, raw_data: &[u8]) -> Result<Signature, BoxError> {
let sig = Signature::from_reader(raw_data)?.swap_remove(0);
if sig.select_sketch(&self.template).is_none() {
Err(format!(
"Could not extract compatible sketch to compare. Expected k={}",
&self.template.ksize(),
)
.into())
} else {
Ok(sig)
}
}
}

async fn search(
ContentLengthLimit(bytes): ContentLengthLimit<Bytes, { 1024 * 5_000 }>, // ~5mb
Extension(state): Extension<SharedState>,
//) -> Result<Json<serde_json::Value>, StatusCode> {
) -> Result<String, StatusCode> {
let sig = parse_sig(&bytes).unwrap();
let matches = state.search(sig).await.unwrap();

Ok(matches.join("\n"))
}

fn parse_sig(raw_data: &[u8]) -> Result<Signature, BoxError> {
let sig = Signature::from_reader(raw_data)?.swap_remove(0);
Ok(sig)
) -> Response<BoxBody> {
let sig = match state.parse_sig(&bytes) {
Ok(sig) => sig,
Err(e) => {
return {
(
StatusCode::BAD_REQUEST,
format!("Error parsing signature: {e}"),
)
.into_response()
}
}
};

match state.search(sig).await {
Ok(matches) => (
StatusCode::OK,
[(header::CONTENT_TYPE, "text/plain; charset=utf-8")],
matches.join("\n"),
)
.into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong: {e}"),
)
.into_response(),
}
}

async fn handle_static_serve_error(error: std::io::Error) -> impl IntoResponse {
Expand Down
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4dca464

Please sign in to comment.