Skip to content

Commit

Permalink
fixup error handling for plaintext error message
Browse files Browse the repository at this point in the history
  • Loading branch information
jr1221 committed Aug 9, 2024
1 parent 67c4a3b commit 17b62c0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
12 changes: 9 additions & 3 deletions scylla-server-rust/src/controllers/car_command_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ pub async fn send_config(
);
// disable scylla if not prod, as there will be None mqtt client
let Some(client) = client else {
return Err(ScyllaError::NotProd);
return Err(ScyllaError::NotProd(
"Car config sending is disabled in mock mode!".to_string(),
));
};

// create a payload object of the values to be parsed by calypso into a CAN packet
Expand All @@ -43,7 +45,9 @@ pub async fn send_config(
payload.data = data;
}
let Ok(bytes) = payload.write_to_bytes() else {
return Err(ScyllaError::ImpossibleEncoding);
return Err(ScyllaError::ImpossibleEncoding(
"Payload could not be written!".to_string(),
));
};

// publish the message to the topic that calypso's encoder is susbcribed to
Expand All @@ -57,7 +61,9 @@ pub async fn send_config(
.await
{
warn!("Could not publish instruction: {}", err);
return Err(ScyllaError::CommFailure);
return Err(ScyllaError::CommFailure(
"Siren publish for instruction failed!".to_string(),
));
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion scylla-server-rust/src/controllers/run_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn get_run_by_id(
let run_data = run_service::get_run_by_id(&db, run_id).await?;

if run_data.is_none() {
return Err(ScyllaError::NotFound);
return Err(ScyllaError::EmptyResult);
}

let run_data_safe = run_data.unwrap();
Expand Down
49 changes: 29 additions & 20 deletions scylla-server-rust/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,49 @@ use tracing::warn;

pub enum ScyllaError {
PrismaError(QueryError),
/// A generic not found for a prisma query
NotFound,
/// Not available in mock mode, which the system is in
NotProd,
NotProd(String),
/// An instruction was not encodable
ImpossibleEncoding,
ImpossibleEncoding(String),
/// Could not communicate to car
CommFailure,
CommFailure(String),
/// A query turned up empty that should not have
EmptyResult,
}

impl From<QueryError> for ScyllaError {
fn from(error: QueryError) -> Self {
warn!("Query error: {:?}", error);
match error {
e if e.is_prisma_error::<RecordNotFound>() => ScyllaError::NotFound,
e => ScyllaError::PrismaError(e),
}
ScyllaError::PrismaError(error)
}
}

// This centralizes all different errors from our app in one place
impl IntoResponse for ScyllaError {
fn into_response(self) -> Response {
let status = match self {
ScyllaError::PrismaError(error) if error.is_prisma_error::<UniqueKeyViolation>() => {
StatusCode::CONFLICT
}
ScyllaError::PrismaError(_) => StatusCode::BAD_REQUEST,
ScyllaError::NotFound => StatusCode::NOT_FOUND,
ScyllaError::NotProd => StatusCode::SERVICE_UNAVAILABLE,
ScyllaError::ImpossibleEncoding => StatusCode::UNPROCESSABLE_ENTITY,
ScyllaError::CommFailure => StatusCode::BAD_GATEWAY,
let (status, reason) = match self {
ScyllaError::PrismaError(error) if error.is_prisma_error::<UniqueKeyViolation>() => (
StatusCode::CONFLICT,
format!("Unique Key Violation: {}", error),
),
ScyllaError::PrismaError(error) if error.is_prisma_error::<RecordNotFound>() => (
StatusCode::NOT_FOUND,
format!("Record Not Found: {}", error),
),
ScyllaError::PrismaError(error) => (
StatusCode::BAD_REQUEST,
format!("Misc query error: {}", error),
),
ScyllaError::NotProd(reason) => (StatusCode::SERVICE_UNAVAILABLE, reason),
ScyllaError::ImpossibleEncoding(reason) => (StatusCode::UNPROCESSABLE_ENTITY, reason),
ScyllaError::CommFailure(reason) => (StatusCode::BAD_GATEWAY, reason),
ScyllaError::EmptyResult => (
StatusCode::NOT_FOUND,
"Fetched an empty result that should not be!".to_string(),
),
};

status.into_response()
warn!("Routing error: {}: {}", status, reason);

(status, reason).into_response()
}
}

0 comments on commit 17b62c0

Please sign in to comment.