Skip to content

Commit

Permalink
fix: add clearer errors (2) (#3755)
Browse files Browse the repository at this point in the history
Co-authored-by: Henry Fontanier <henry@dust.tt>
  • Loading branch information
fontanierh and Henry Fontanier authored Feb 15, 2024
1 parent 2bcc595 commit f743fdc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion core/src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub enum QueryDatabaseError {
impl From<SqliteWorkerError> for QueryDatabaseError {
fn from(e: SqliteWorkerError) -> Self {
match &e {
SqliteWorkerError::ServerError(code, _) => match code.as_str() {
SqliteWorkerError::ServerError(_, code, _) => match code.as_str() {
"too_many_result_rows" => QueryDatabaseError::TooManyResultRows,
_ => e.into(),
},
Expand Down
34 changes: 17 additions & 17 deletions core/src/sqlite_workers/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ pub struct SqliteWorker {

#[derive(Debug, Error)]
pub enum SqliteWorkerError {
#[error("SqliteWorkerError Server error (code={0}, status={1})")]
ServerError(String, u16),
#[error("SqliteWorkerError Unexpected server error (status={0}): {1}")]
UnexpectedServerError(u16, String),
#[error("SqliteWorkerError Body parsing error (status={1}): {0}")]
BodyParsingError(serde_json::Error, u16),
#[error("SqliteWorkerError Server error (uri={0}, code={1}, status={2})")]
ServerError(String, String, u16),
#[error("SqliteWorkerError Unexpected server error (uri={0}, status={1}): {2}")]
UnexpectedServerError(String, u16, String),
#[error("SqliteWorkerError Body parsing error (uri={1}, status={2}): {0}")]
BodyParsingError(serde_json::Error, String, u16),
#[error("SqliteWorkerError HyperError: {0}")]
HyperError(#[from] hyper::Error),
#[error("SqliteWorkerError Unexpected error: {0}")]
Expand Down Expand Up @@ -87,9 +87,7 @@ impl SqliteWorker {
.to_string(),
))?;

let res = Client::new().request(req).await?;

let body_bytes = get_response_body(res).await?;
let body_bytes = get_response_body(req).await?;

#[derive(Deserialize)]
struct ExecuteQueryResponseBody {
Expand Down Expand Up @@ -121,9 +119,7 @@ impl SqliteWorker {
.uri(format!("{}/databases/{}", worker_url, database_unique_id))
.body(Body::from(""))?;

let res = Client::new().request(req).await?;

let _ = get_response_body(res).await?;
let _ = get_response_body(req).await?;

Ok(())
}
Expand All @@ -136,22 +132,25 @@ impl SqliteWorker {
.uri(format!("{}/databases", worker_url))
.body(Body::from(""))?;

let res = Client::new().request(req).await?;
let _ = get_response_body(res).await?;
let _ = get_response_body(req).await?;

Ok(())
}
}

async fn get_response_body(res: hyper::Response<hyper::Body>) -> Result<Bytes, SqliteWorkerError> {
async fn get_response_body(req: hyper::Request<hyper::Body>) -> Result<Bytes, SqliteWorkerError> {
let uri = req.uri().to_string();
let res = Client::new().request(req).await?;

let status = res.status().as_u16();
let body = hyper::body::to_bytes(res.into_body()).await?;

match status {
200 => Ok(body),
s => {
let body_json: serde_json::Value = serde_json::from_slice(&body)
.map_err(|e| SqliteWorkerError::BodyParsingError(e, s))?;
.map_err(|e| SqliteWorkerError::BodyParsingError(e, uri.clone(), s))?;

let error = body_json.get("error");
let error_code = match error {
Some(e) => e
Expand All @@ -162,8 +161,9 @@ async fn get_response_body(res: hyper::Response<hyper::Body>) -> Result<Bytes, S
None => None,
};
match error_code {
Some(code) => Err(SqliteWorkerError::ServerError(code.to_string(), s))?,
Some(code) => Err(SqliteWorkerError::ServerError(uri, code.to_string(), s))?,
None => Err(SqliteWorkerError::UnexpectedServerError(
uri,
s,
format!("No error code in response: {}", body_json.to_string()),
))?,
Expand Down

0 comments on commit f743fdc

Please sign in to comment.