Skip to content

Commit

Permalink
better sql error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Nov 21, 2024
1 parent ff49502 commit 7d12dba
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions crates/torii/server/src/handlers/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ impl SqlHandler {
})
.collect();

Check warning on line 59 in crates/torii/server/src/handlers/sql.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/server/src/handlers/sql.rs#L57-L59

Added lines #L57 - L59 were not covered by tests

let json = serde_json::to_string(&result).unwrap();
let json = match serde_json::to_string(&result) {
Ok(json) => json,
Err(e) => {
return Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from(format!("Failed to serialize result: {:?}", e)))
.unwrap();

Check warning on line 67 in crates/torii/server/src/handlers/sql.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/server/src/handlers/sql.rs#L61-L67

Added lines #L61 - L67 were not covered by tests
}
};

Response::builder()
.status(StatusCode::OK)
Expand All @@ -78,14 +86,24 @@ impl SqlHandler {
Method::GET => {
// Get the query from the query params
let params = req.uri().query().unwrap_or_default();
Ok(form_urlencoded::parse(params.as_bytes())
form_urlencoded::parse(params.as_bytes())
.find(|(key, _)| key == "q" || key == "query")
.map(|(_, value)| value.to_string())
.unwrap_or_default())
.ok_or(
Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from("Missing 'q' or 'query' parameter."))
.unwrap(),
)

Check warning on line 97 in crates/torii/server/src/handlers/sql.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/server/src/handlers/sql.rs#L88-L97

Added lines #L88 - L97 were not covered by tests
}
Method::POST => {
// Get the query from request body
let body_bytes = hyper::body::to_bytes(req.into_body()).await.unwrap_or_default();
let body_bytes = hyper::body::to_bytes(req.into_body()).await.map_err(|_| {
Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from("Failed to read query from request body"))
.unwrap()
})?;
String::from_utf8(body_bytes.to_vec()).map_err(|_| {
Response::builder()
.status(StatusCode::BAD_REQUEST)
Expand Down

0 comments on commit 7d12dba

Please sign in to comment.