Skip to content

Commit

Permalink
editoast: stdcm 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 20, 2025
1 parent b1c5f94 commit 776f9b3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
18 changes: 12 additions & 6 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6112,10 +6112,13 @@ components:
context:
type: object
required:
- message
- expected_min
- provided_consist_length
properties:
message:
type: string
expected_min:
type: number
provided_consist_length:
type: number
message:
type: string
status:
Expand All @@ -6136,10 +6139,13 @@ components:
context:
type: object
required:
- message
- expected_min
- provided_consist_mass
properties:
message:
type: string
expected_min:
type: number
provided_consist_mass:
type: number
message:
type: string
status:
Expand Down
31 changes: 21 additions & 10 deletions editoast/src/views/timetable/stdcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,18 @@ 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 {provided_consist_mass}: it should be greater than {expected_min}"
)]
InvalidConsistMass {
provided_consist_mass: f64,
expected_min: f64,
},
#[error("Invalid consist length {provided_consist_length}: it should be greater than {expected_min}")]
InvalidConsistLength {
provided_consist_length: f64,
expected_min: f64,
},
}

#[derive(Debug, Default, Clone, Serialize, Deserialize, IntoParams, ToSchema)]
Expand Down Expand Up @@ -964,9 +972,12 @@ mod tests {
.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.error_type,
"editoast:stdcm_v2:InvalidConsistMass".to_string()
);
assert_eq!(
stdcm_response.context["expected_min"].as_f64(),
Some(900000.0)
);
}

Expand Down Expand Up @@ -1010,10 +1021,10 @@ mod tests {
.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.error_type,
"editoast:stdcm_v2:InvalidConsistLength".to_string()
);
assert_eq!(stdcm_response.context["expected_min"].as_f64(), Some(400.0));
}

#[rstest]
Expand Down
13 changes: 4 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,8 @@ 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),
),
expected_min: consist_mass.value,
provided_consist_mass: request_total_mass.value,
}
.into());
}
Expand All @@ -354,10 +351,8 @@ 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),
),
expected_min: consist_length.value,
provided_consist_length: request_total_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 {{provided_consist_mass}}: it should be greater than '{{expected_min}}'",
"InvalidConsistLength": "Invalid consist length {{provided_consist_length}}: it should be greater than '{{expected_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": "Masse du convoi invalide {{provided_consist_mass}} : elle doit être supérieure à '{{expected_min}}",
"InvalidConsistLength": "Longueur du convoi invalide {{provided_consist_length}} : doit être supérieure à '{{expected_min}}'"
},
"study": {
"Database": "Erreur interne (base de données)",
Expand Down

0 comments on commit 776f9b3

Please sign in to comment.