-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy patherrors.rs
163 lines (144 loc) · 4.95 KB
/
errors.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::str::FromStr;
use axum::extract::Json;
use axum::extract::Path;
use axum::extract::Query;
use axum::extract::State;
use axum::Extension;
use editoast_authz::BuiltinRole;
use editoast_derive::EditoastError;
use editoast_schemas::primitives::Identifier;
use serde::Deserialize;
use serde::Serialize;
use thiserror::Error;
use crate::error::Result;
use crate::generated_data::infra_error::InfraError;
use crate::generated_data::infra_error::InfraErrorTypeLabel;
use crate::models::infra::errors::Level;
use crate::models::prelude::*;
use crate::models::Infra;
use crate::views::infra::InfraIdParam;
use crate::views::pagination::PaginationQueryParams;
use crate::views::pagination::PaginationStats;
use crate::views::AuthenticationExt;
use crate::views::AuthorizationError;
use editoast_models::DbConnectionPoolV2;
use super::InfraApiError;
crate::routes! {
"/errors" => list_errors,
}
#[derive(Debug, Clone, Deserialize, utoipa::IntoParams)]
#[into_params(parameter_in = Query)]
struct ErrorListQueryParams {
/// Whether the response should include errors or warnings
#[serde(default)]
#[param(inline)]
level: Level,
/// The type of error to filter on
#[param(value_type = Option<InfraErrorTypeLabel>)]
error_type: Option<String>,
/// Filter errors and warnings related to a given object
#[param(value_type = Option<String>)]
object_id: Option<Identifier>,
}
#[derive(Serialize, utoipa::ToSchema)]
#[cfg_attr(test, derive(Debug, Deserialize, PartialEq))]
pub(in crate::views) struct ErrorListResponse {
#[serde(flatten)]
pub(in crate::views) stats: PaginationStats,
#[schema(inline)]
pub(in crate::views) results: Vec<InfraErrorResponse>,
}
#[derive(Debug, Serialize, utoipa::ToSchema)]
#[cfg_attr(test, derive(Deserialize, PartialEq))]
pub(in crate::views) struct InfraErrorResponse {
pub(in crate::views) information: InfraError,
}
/// A paginated list of errors related to an infra
#[utoipa::path(
get, path = "",
tag = "infra",
params(InfraIdParam, PaginationQueryParams, ErrorListQueryParams),
responses(
(status = 200, body = inline(ErrorListResponse), description = "A paginated list of errors"),
),
)]
async fn list_errors(
State(db_pool): State<DbConnectionPoolV2>,
Extension(auth): AuthenticationExt,
Path(InfraIdParam { infra_id }): Path<InfraIdParam>,
Query(pagination_params): Query<PaginationQueryParams>,
Query(ErrorListQueryParams {
level,
error_type,
object_id,
}): Query<ErrorListQueryParams>,
) -> Result<Json<ErrorListResponse>> {
let authorized = auth
.check_roles([BuiltinRole::InfraRead].into())
.await
.map_err(AuthorizationError::AuthError)?;
if !authorized {
return Err(AuthorizationError::Forbidden.into());
}
let (page, page_size) = pagination_params
.validate(100)?
.warn_page_size(100)
.unpack();
let (page, page_size) = (page as u64, page_size as u64);
let error_type = match error_type.map(|et| InfraErrorTypeLabel::from_str(&et).ok()) {
Some(None) => return Err(ListErrorsErrors::WrongErrorTypeProvided.into()),
Some(et) => et,
None => None,
};
let conn = &mut db_pool.get().await?;
let infra =
Infra::retrieve_or_fail(conn, infra_id, || InfraApiError::NotFound { infra_id }).await?;
let (results, total_count) = infra
.get_paginated_errors(conn, level, error_type, object_id, page, page_size)
.await?;
let results = results
.into_iter()
.map(|information| InfraErrorResponse { information })
.collect::<Vec<_>>();
let stats = PaginationStats::new(results.len() as u64, total_count, page, page_size);
Ok(Json(ErrorListResponse { stats, results }))
}
#[derive(Debug, Error, EditoastError)]
#[editoast_error(base_id = "infra:errors")]
enum ListErrorsErrors {
#[error("Wrong Error type provided")]
WrongErrorTypeProvided,
}
#[cfg(test)]
pub(in crate::views) async fn query_errors(
conn: &mut editoast_models::DbConnection,
infra: &Infra,
) -> (Vec<InfraError>, u64) {
infra
.get_paginated_errors(conn, Level::All, None, None, 1, 10000)
.await
.expect("errors should be fetched successfully")
}
#[cfg(test)]
mod tests {
use axum::http::StatusCode;
use rstest::rstest;
use crate::models::fixtures::create_empty_infra;
use crate::views::test_app::TestAppBuilder;
#[rstest]
async fn list_errors_get() {
let app = TestAppBuilder::default_app();
let db_pool = app.db_pool();
let empty_infra = create_empty_infra(&mut db_pool.get_ok()).await;
let error_type = "overlapping_electrifications";
let level = "warnings";
let req = app.get(
format!(
"/infra/{}/errors?error_type={}&level={}",
empty_infra.id, error_type, level
)
.as_str(),
);
app.fetch(req).assert_status(StatusCode::OK);
}
}