diff --git a/scylla-server-rust/src/controllers/car_command_controller.rs b/scylla-server-rust/src/controllers/car_command_controller.rs index d1646c6f..0e9557e9 100644 --- a/scylla-server-rust/src/controllers/car_command_controller.rs +++ b/scylla-server-rust/src/controllers/car_command_controller.rs @@ -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 @@ -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 @@ -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(()) diff --git a/scylla-server-rust/src/controllers/run_controller.rs b/scylla-server-rust/src/controllers/run_controller.rs index 984f6db2..528c4405 100644 --- a/scylla-server-rust/src/controllers/run_controller.rs +++ b/scylla-server-rust/src/controllers/run_controller.rs @@ -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(); diff --git a/scylla-server-rust/src/error.rs b/scylla-server-rust/src/error.rs index 216873ca..a0c09895 100644 --- a/scylla-server-rust/src/error.rs +++ b/scylla-server-rust/src/error.rs @@ -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 for ScyllaError { fn from(error: QueryError) -> Self { - warn!("Query error: {:?}", error); - match error { - e if e.is_prisma_error::() => 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::() => { - 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::() => ( + StatusCode::CONFLICT, + format!("Unique Key Violation: {}", error), + ), + ScyllaError::PrismaError(error) if error.is_prisma_error::() => ( + 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() } }