Skip to content

Commit

Permalink
editoast: lmr improve validation messages
Browse files Browse the repository at this point in the history
Signed-off-by: Egor Berezovskiy <[email protected]>
  • Loading branch information
Wadjetz committed Feb 18, 2025
1 parent 3e66137 commit 1ff3603
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
12 changes: 6 additions & 6 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6112,10 +6112,10 @@ components:
context:
type: object
required:
- message
- min
properties:
message:
type: string
min:
type: number
message:
type: string
status:
Expand All @@ -6136,10 +6136,10 @@ components:
context:
type: object
required:
- message
- min
properties:
message:
type: string
min:
type: number
message:
type: string
status:
Expand Down
25 changes: 11 additions & 14 deletions editoast/src/views/timetable/stdcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ enum StdcmResponse {
},
}

#[derive(Debug, Error, EditoastError, Serialize)]
#[derive(Debug, Error, EditoastError, Serialize, Deserialize, PartialEq)]
#[editoast_error(base_id = "stdcm_v2")]
enum StdcmError {
#[error("Infrastrcture {infra_id} does not exist")]
Expand All @@ -99,10 +99,10 @@ enum StdcmError {
TrainSimulationFail,
#[error("Path items are invalid")]
InvalidPathItems { items: Vec<InvalidPathItem> },
#[error("Invalid consist mass: {message}")]
InvalidConsistMass { message: String },
#[error("Invalid consist length: {message}")]
InvalidConsistLength { message: String },
#[error("Invalid consist mass: it should be greater than {min}")]
InvalidConsistMass { min: f64 },
#[error("Invalid consist length: it should be greater than {min}")]
InvalidConsistLength { min: f64 },
}

#[derive(Debug, Default, Clone, Serialize, Deserialize, IntoParams, ToSchema)]
Expand Down Expand Up @@ -545,7 +545,6 @@ mod tests {
use crate::core::simulation::PhysicsConsist;
use crate::core::simulation::ReportTrain;
use crate::core::simulation::SpeedLimitProperties;
use crate::error::InternalError;
use crate::models::fixtures::create_fast_rolling_stock;
use crate::models::fixtures::create_simple_rolling_stock;
use crate::models::fixtures::create_small_infra;
Expand Down Expand Up @@ -949,15 +948,14 @@ mod tests {
.post(format!("/timetable/{}/stdcm?infra={}", timetable.id, small_infra.id).as_str())
.json(&get_stdcm_payload(rolling_stock.id, None, total_mass, None));

let stdcm_response: InternalError = app
let stdcm_response: StdcmError = app
.fetch(request)
.assert_status(StatusCode::BAD_REQUEST)
.json_into();

assert_eq!(
stdcm_response.message,
"Invalid consist mass: The total mass must be greater than the sum of the rolling stock masses (900000 kilograms)"
.to_owned()
stdcm_response,
StdcmError::InvalidConsistMass { min: 900000.0 }
);
}

Expand Down Expand Up @@ -995,15 +993,14 @@ mod tests {
total_length,
));

let stdcm_response: InternalError = app
let stdcm_response: StdcmError = app
.fetch(request)
.assert_status(StatusCode::BAD_REQUEST)
.json_into();

assert_eq!(
stdcm_response.message,
"Invalid consist length: The total length must be greater than the sum of the rolling stock lengths (400 meters)"
.to_owned()
stdcm_response,
StdcmError::InvalidConsistLength { min: 400 }
);
}

Expand Down
11 changes: 2 additions & 9 deletions editoast/src/views/timetable/stdcm/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
use units::quantities;
use uom::fmt::DisplayStyle;
use uom::si::length::meter;
use uom::si::mass::kilogram;
use utoipa::ToSchema;
Expand Down Expand Up @@ -327,10 +326,7 @@ impl Request {
if let Some(request_total_mass) = self.total_mass {
if request_total_mass < consist_mass {
return Err(StdcmError::InvalidConsistMass {
message: format!(
"The total mass must be greater than the sum of the rolling stock masses ({})",
&consist_mass.into_format_args(kilogram, DisplayStyle::Description),
),
min: consist_mass.value,
}
.into());
}
Expand All @@ -354,10 +350,7 @@ impl Request {
if let Some(request_total_length) = self.total_length {
if request_total_length < consist_length {
return Err(StdcmError::InvalidConsistLength {
message: format!(
"The total length must be greater than the sum of the rolling stock lengths ({})",
&consist_length.into_format_args(meter, DisplayStyle::Description),
),
min: consist_length.value,
}
.into());
}
Expand Down
4 changes: 2 additions & 2 deletions front/public/locales/en/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@
"TowedRollingStockNotFound": "Towed rolling stock {towed_rolling_stock_id} does not exist",
"TrainSimulationFail": "Train simulation failed",
"TimetableNotFound": "Timetable '{{timetable_id}}' does not exist",
"InvalidConsistMass": "Invalid consist mass: '{{message}}'",
"InvalidConsistLength": "Invalid consist length: '{{message}}'"
"InvalidConsistMass": "Invalid consist mass: it should be greater than '{{min}}'",
"InvalidConsistLength": "Invalid consist length: it should be greater than '{{min}}'"
},
"study": {
"Database": "Internal error (database)",
Expand Down
4 changes: 2 additions & 2 deletions front/public/locales/fr/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@
"TowedRollingStockNotFound": "Matériel roulant remorqué {towed_rolling_stock_id} non trouvé",
"TrainSimulationFail": "Échec de la simulation du train",
"TimetableNotFound": "Grille horaire '{{timetable_id}}' non trouvée",
"InvalidConsistMass": "La masse du convoi est invalide : '{{message}}'",
"InvalidConsistLength": "La longueur du convoi est invalide : '{{message}}'"
"InvalidConsistMass": "La masse du convoi doit être supérieure à '{{min}}'",
"InvalidConsistLength": "La longueur du convoi doit être supérieure à '{{min}}'"
},
"study": {
"Database": "Erreur interne (base de données)",
Expand Down

0 comments on commit 1ff3603

Please sign in to comment.