diff --git a/editoast/openapi.yaml b/editoast/openapi.yaml index 685843d90f7..be97e2d265d 100644 --- a/editoast/openapi.yaml +++ b/editoast/openapi.yaml @@ -735,6 +735,60 @@ paths: application/json: schema: $ref: '#/components/schemas/ElectricalProfileSet' + '500': + description: '' + content: + application/json: + schema: + oneOf: + - type: object + description: 'database error: {0}' + required: + - type + - status + - message + - context + properties: + context: + type: object + message: + type: string + status: + type: integer + format: int32 + enum: + - '500' + type: + type: string + enum: + - editoast:database:internal + '503': + description: '' + content: + application/json: + schema: + oneOf: + - type: object + description: 'database unavailable: {0}' + required: + - type + - status + - message + - context + properties: + context: + type: object + message: + type: string + status: + type: integer + format: int32 + enum: + - '503' + type: + type: string + enum: + - editoast:database:unavailable /electrical_profile_set/{electrical_profile_set_id}: get: tags: @@ -754,6 +808,93 @@ paths: application/json: schema: $ref: '#/components/schemas/ElectricalProfileSetData' + '404': + description: '' + content: + application/json: + schema: + oneOf: + - type: object + description: Electrical Profile Set '{electrical_profile_set_id}', could not be found + required: + - type + - status + - message + - context + properties: + context: + type: object + required: + - electrical_profile_set_id + properties: + electrical_profile_set_id: + type: integer + format: int64 + message: + type: string + status: + type: integer + format: int32 + enum: + - '404' + type: + type: string + enum: + - editoast:document:not_found + '500': + description: '' + content: + application/json: + schema: + oneOf: + - type: object + description: 'database error: {0}' + required: + - type + - status + - message + - context + properties: + context: + type: object + message: + type: string + status: + type: integer + format: int32 + enum: + - '500' + type: + type: string + enum: + - editoast:database:internal + '503': + description: '' + content: + application/json: + schema: + oneOf: + - type: object + description: 'database unavailable: {0}' + required: + - type + - status + - message + - context + properties: + context: + type: object + message: + type: string + status: + type: integer + format: int32 + enum: + - '503' + type: + type: string + enum: + - editoast:database:unavailable delete: tags: - electrical_profiles @@ -768,6 +909,60 @@ paths: responses: '204': description: The electrical profile was deleted successfully + '500': + description: '' + content: + application/json: + schema: + oneOf: + - type: object + description: 'database error: {0}' + required: + - type + - status + - message + - context + properties: + context: + type: object + message: + type: string + status: + type: integer + format: int32 + enum: + - '500' + type: + type: string + enum: + - editoast:database:internal + '503': + description: '' + content: + application/json: + schema: + oneOf: + - type: object + description: 'database unavailable: {0}' + required: + - type + - status + - message + - context + properties: + context: + type: object + message: + type: string + status: + type: integer + format: int32 + enum: + - '503' + type: + type: string + enum: + - editoast:database:unavailable /electrical_profile_set/{electrical_profile_set_id}/level_order: get: tags: @@ -800,6 +995,93 @@ paths: - 25000V - 22500V - 20000V + '404': + description: '' + content: + application/json: + schema: + oneOf: + - type: object + description: Electrical Profile Set '{electrical_profile_set_id}', could not be found + required: + - type + - status + - message + - context + properties: + context: + type: object + required: + - electrical_profile_set_id + properties: + electrical_profile_set_id: + type: integer + format: int64 + message: + type: string + status: + type: integer + format: int32 + enum: + - '404' + type: + type: string + enum: + - editoast:document:not_found + '500': + description: '' + content: + application/json: + schema: + oneOf: + - type: object + description: 'database error: {0}' + required: + - type + - status + - message + - context + properties: + context: + type: object + message: + type: string + status: + type: integer + format: int32 + enum: + - '500' + type: + type: string + enum: + - editoast:database:internal + '503': + description: '' + content: + application/json: + schema: + oneOf: + - type: object + description: 'database unavailable: {0}' + required: + - type + - status + - message + - context + properties: + context: + type: object + message: + type: string + status: + type: integer + format: int32 + enum: + - '503' + type: + type: string + enum: + - editoast:database:unavailable /fonts/{font}/{glyph}: get: tags: @@ -7460,51 +7742,6 @@ components: type: string enum: - editoast:infra:edition:SplitTrackSectionBadOffset - EditoastElectricalProfilesErrorDatabase: - type: object - required: - - type - - status - - message - properties: - context: - type: object - title: EditoastElectricalProfilesErrorDatabaseContext - message: - type: string - status: - type: integer - enum: - - 500 - type: - type: string - enum: - - editoast:electrical_profiles:Database - EditoastElectricalProfilesErrorNotFound: - type: object - required: - - type - - status - - message - properties: - context: - type: object - title: EditoastElectricalProfilesErrorNotFoundContext - required: - - electrical_profile_set_id - properties: - electrical_profile_set_id: - type: integer - message: - type: string - status: - type: integer - enum: - - 404 - type: - type: string - enum: - - editoast:electrical_profiles:NotFound EditoastError: oneOf: - $ref: '#/components/schemas/EditoastAbortJoinError' @@ -7542,8 +7779,6 @@ components: - $ref: '#/components/schemas/EditoastDelimitedAreaErrorInvalidLocations' - $ref: '#/components/schemas/EditoastEditionErrorInfraIsLocked' - $ref: '#/components/schemas/EditoastEditionErrorSplitTrackSectionBadOffset' - - $ref: '#/components/schemas/EditoastElectricalProfilesErrorDatabase' - - $ref: '#/components/schemas/EditoastElectricalProfilesErrorNotFound' - $ref: '#/components/schemas/EditoastErrorDatabase' - $ref: '#/components/schemas/EditoastErrorInfraNotFound' - $ref: '#/components/schemas/EditoastErrorInvalidInput' diff --git a/editoast/src/views.rs b/editoast/src/views.rs index dec74c48340..7aa85ff3353 100644 --- a/editoast/src/views.rs +++ b/editoast/src/views.rs @@ -31,6 +31,7 @@ mod version; pub mod work_schedules; mod worker_load; +use editoast_derive::ViewError; pub use server::*; #[cfg(test)] @@ -507,6 +508,16 @@ where } } +#[derive(Debug, thiserror::Error, ViewError)] +pub(in crate::views) enum DatabaseError { + #[error("database error: {0}")] + #[view_error(status = INTERNAL_SERVER_ERROR)] + Internal(#[from] models::Error), + #[error("database unavailable: {0}")] + #[view_error(status = SERVICE_UNAVAILABLE)] + Unavailable(#[from] database::DatabasePoolError), +} + #[cfg(test)] mod tests { use axum::http::StatusCode; diff --git a/editoast/src/views/documents.rs b/editoast/src/views/documents.rs index 36c21a59365..4b17eb621fc 100644 --- a/editoast/src/views/documents.rs +++ b/editoast/src/views/documents.rs @@ -14,20 +14,11 @@ use utoipa::ToSchema; use crate::error::Result; use crate::views::ContentType; +use crate::views::DatabaseError; use database::DbConnectionPoolV2; use models::Document; use models::prelude::*; -#[derive(Debug, thiserror::Error, ViewError)] -pub(in crate::views) enum DatabaseError { - #[error("database error: {0}")] - #[view_error(status = INTERNAL_SERVER_ERROR)] - Internal(#[from] models::Error), - #[error("database unavailable: {0}")] - #[view_error(status = SERVICE_UNAVAILABLE)] - Unavailable(#[from] database::DatabasePoolError), -} - #[derive(Debug, thiserror::Error, ViewError)] #[error("Document '{document_key}' not found")] #[view_error(status = NOT_FOUND, context, path = document::not_found)] diff --git a/editoast/src/views/electrical_profiles.rs b/editoast/src/views/electrical_profiles.rs index 2d6710ab1d8..7cbf47c74ba 100644 --- a/editoast/src/views/electrical_profiles.rs +++ b/editoast/src/views/electrical_profiles.rs @@ -9,7 +9,8 @@ use axum::extract::State; use axum::http::StatusCode; use axum::response::IntoResponse; use database::DbConnectionPoolV2; -use editoast_derive::EditoastError; +use derive_more::From; +use editoast_derive::ViewError; use schemas::infra::ElectricalProfileSetData; use schemas::infra::LevelValues; use serde::Deserialize; @@ -17,6 +18,7 @@ use thiserror::Error; use utoipa::IntoParams; use crate::error::Result; +use crate::views::DatabaseError; use models::ElectricalProfileSet; use models::LightElectricalProfileSet; use models::prelude::*; @@ -51,17 +53,20 @@ pub(in crate::views) async fn list( params(ElectricalProfileSetId), responses( (status = 200, body = ElectricalProfileSetData, description = "The list of electrical profiles in the set"), + ElectricalProfilesError ) )] pub(in crate::views) async fn get( State(db_pool): State>, Path(electrical_profile_set_id): Path, -) -> Result> { +) -> Result, ElectricalProfilesError> { let ep_set = ElectricalProfileSet::retrieve_or_fail( db_pool.get().await?, electrical_profile_set_id, - || ElectricalProfilesError::NotFound { - electrical_profile_set_id, + || { + ElectricalProfilesError::NotFound(ElectricalProfileNotFound { + electrical_profile_set_id, + }) }, ) .await?; @@ -83,17 +88,20 @@ pub(in crate::views) async fn get( "25000V": ["25000V", "22500V", "20000V"] }) ), + ElectricalProfilesError ) )] pub(in crate::views) async fn get_level_order( State(db_pool): State>, Path(electrical_profile_set_id): Path, -) -> Result>> { +) -> Result>, ElectricalProfilesError> { let ep_set = ElectricalProfileSet::retrieve_or_fail( db_pool.get().await?, electrical_profile_set_id, - || ElectricalProfilesError::NotFound { - electrical_profile_set_id, + || { + ElectricalProfilesError::NotFound(ElectricalProfileNotFound { + electrical_profile_set_id, + }) }, ) .await?; @@ -108,12 +116,13 @@ pub(in crate::views) async fn get_level_order( params(ElectricalProfileSetId), responses( (status = 204, description = "The electrical profile was deleted successfully"), + DatabaseError ) )] pub(in crate::views) async fn delete( State(db_pool): State>, Path(electrical_profile_set_id): Path, -) -> Result { +) -> Result { let conn = &mut db_pool.get().await?; let deleted = ElectricalProfileSet::delete_static(conn, electrical_profile_set_id).await?; if deleted { @@ -138,13 +147,14 @@ pub(in crate::views) struct ElectricalProfileQueryArgs { request_body = ElectricalProfileSetData, responses( (status = 200, body = ElectricalProfileSet, description = "The list of ids and names of electrical profile sets available"), + DatabaseError ) )] pub(in crate::views) async fn post_electrical_profile( State(db_pool): State>, Query(ep_set_name): Query, Json(ep_data): Json, -) -> Result> { +) -> Result, DatabaseError> { let ep_set = ElectricalProfileSet::changeset() .name(ep_set_name.name) .data(ep_data); @@ -152,16 +162,24 @@ pub(in crate::views) async fn post_electrical_profile( Ok(Json(ep_set.create(conn).await?)) } -#[derive(Debug, Error, EditoastError)] -#[editoast_error(base_id = "electrical_profiles")] -pub enum ElectricalProfilesError { - /// Couldn't find the electrical profile set with the given id - #[error("Electrical Profile Set '{electrical_profile_set_id}', could not be found")] - #[editoast_error(status = 404)] - NotFound { electrical_profile_set_id: i64 }, +#[derive(Debug, thiserror::Error, ViewError)] +#[error("Electrical Profile Set '{electrical_profile_set_id}', could not be found")] +#[view_error(status = NOT_FOUND, context, path = document::not_found)] +pub(in crate::views) struct ElectricalProfileNotFound { + electrical_profile_set_id: i64, +} + +#[derive(Debug, Error, ViewError, From)] +pub(in crate::views) enum ElectricalProfilesError { + #[error(transparent)] + NotFound( + #[from] + #[view_error] + ElectricalProfileNotFound, + ), #[error(transparent)] - #[editoast_error(status = 500)] - Database(#[from] models::Error), + #[from(forward)] + Database(#[view_error] DatabaseError), } #[cfg(test)] diff --git a/osrd_schemas/osrd_schemas/models.py b/osrd_schemas/osrd_schemas/models.py index 207f74c6926..f27c89bbcaa 100644 --- a/osrd_schemas/osrd_schemas/models.py +++ b/osrd_schemas/osrd_schemas/models.py @@ -913,34 +913,6 @@ class EditoastEditionErrorSplitTrackSectionBadOffset(BaseModel): ) -class EditoastElectricalProfilesErrorDatabase(BaseModel): - context: Annotated[ - dict[str, Any] | None, - Field(title="EditoastElectricalProfilesErrorDatabaseContext"), - ] = None - message: str - status: Literal[500] = 500 - type: Literal["editoast:electrical_profiles:Database"] = ( - "editoast:electrical_profiles:Database" - ) - - -class EditoastElectricalProfilesErrorNotFoundContext(BaseModel): - electrical_profile_set_id: int - - -class EditoastElectricalProfilesErrorNotFound(BaseModel): - context: Annotated[ - EditoastElectricalProfilesErrorNotFoundContext | None, - Field(title="EditoastElectricalProfilesErrorNotFoundContext"), - ] = None - message: str - status: Literal[404] = 404 - type: Literal["editoast:electrical_profiles:NotFound"] = ( - "editoast:electrical_profiles:NotFound" - ) - - class EditoastErrorDatabase(BaseModel): context: Annotated[ dict[str, Any] | None, Field(title="EditoastErrorDatabaseContext") @@ -4989,8 +4961,6 @@ class EditoastError( | EditoastDelimitedAreaErrorInvalidLocations | EditoastEditionErrorInfraIsLocked | EditoastEditionErrorSplitTrackSectionBadOffset - | EditoastElectricalProfilesErrorDatabase - | EditoastElectricalProfilesErrorNotFound | EditoastErrorDatabase | EditoastErrorInfraNotFound | EditoastErrorInvalidInput @@ -5167,8 +5137,6 @@ class EditoastError( | EditoastDelimitedAreaErrorInvalidLocations | EditoastEditionErrorInfraIsLocked | EditoastEditionErrorSplitTrackSectionBadOffset - | EditoastElectricalProfilesErrorDatabase - | EditoastElectricalProfilesErrorNotFound | EditoastErrorDatabase | EditoastErrorInfraNotFound | EditoastErrorInvalidInput