This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: better server-side errors #9
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
|
@@ -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(), | ||
), | ||
Comment on lines
+66
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
..Default::default() | ||
}, | ||
)); | ||
|
@@ -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)| { | ||
|
@@ -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 { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disabling this for now because it has errors with
rocksdb 0.18
(due tobz_internal_error
duplication when linking). It is already fixed (more info) but need to update sourmash-bio/sourmash#2230 to use the newer rocksdb version.