feat(api): improve error handling
This commit is contained in:
parent
85779086bb
commit
8d1fe3b8f2
@ -51,6 +51,9 @@ pub async fn pdf() -> impl IntoResponse {
|
|||||||
|
|
||||||
async fn download_pdf(id: u8) -> Result<String, Error> {
|
async fn download_pdf(id: u8) -> Result<String, Error> {
|
||||||
let links = get_pdf_links().await.unwrap();
|
let links = get_pdf_links().await.unwrap();
|
||||||
|
if id as usize >= links.len() {
|
||||||
|
return Err(failure::err_msg("Invalid ID"));
|
||||||
|
}
|
||||||
let url = &links[id as usize];
|
let url = &links[id as usize];
|
||||||
|
|
||||||
let mut hasher = Sha1::new();
|
let mut hasher = Sha1::new();
|
||||||
@ -92,7 +95,15 @@ pub struct Header {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_classes(Query(params): Query<FilterByTeacherQuery>) -> impl IntoResponse {
|
pub async fn get_classes(Query(params): Query<FilterByTeacherQuery>) -> impl IntoResponse {
|
||||||
let filename = download_pdf(params.id).await.unwrap();
|
let filename = match download_pdf(params.id).await {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(_) => {
|
||||||
|
return (
|
||||||
|
StatusCode::UNPROCESSABLE_ENTITY,
|
||||||
|
Json(json!({ "errore": "ID non valido"})),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
let csv_content = fs::read_to_string(&filename).unwrap();
|
let csv_content = fs::read_to_string(&filename).unwrap();
|
||||||
|
|
||||||
let mut rdr = csv::Reader::from_reader(csv_content.as_bytes());
|
let mut rdr = csv::Reader::from_reader(csv_content.as_bytes());
|
||||||
@ -126,7 +137,15 @@ pub struct FilterByClassQuery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_teachers(Query(params): Query<FilterByClassQuery>) -> impl IntoResponse {
|
pub async fn get_teachers(Query(params): Query<FilterByClassQuery>) -> impl IntoResponse {
|
||||||
let filename = download_pdf(params.id).await.unwrap();
|
let filename = match download_pdf(params.id).await {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(_) => {
|
||||||
|
return (
|
||||||
|
StatusCode::UNPROCESSABLE_ENTITY,
|
||||||
|
Json(json!({ "errore": "ID non valido"})),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
let csv_content = fs::read_to_string(&filename).unwrap();
|
let csv_content = fs::read_to_string(&filename).unwrap();
|
||||||
|
|
||||||
let mut rdr = csv::Reader::from_reader(csv_content.as_bytes());
|
let mut rdr = csv::Reader::from_reader(csv_content.as_bytes());
|
||||||
@ -135,7 +154,15 @@ pub async fn get_teachers(Query(params): Query<FilterByClassQuery>) -> impl Into
|
|||||||
let mut result: HashMap<String, Vec<String>> = HashMap::new();
|
let mut result: HashMap<String, Vec<String>> = HashMap::new();
|
||||||
|
|
||||||
for record in rdr.records() {
|
for record in rdr.records() {
|
||||||
let record = record.unwrap();
|
let record = match record {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(_) => {
|
||||||
|
return (
|
||||||
|
StatusCode::UNPROCESSABLE_ENTITY,
|
||||||
|
Json(json!({ "errore": "Formato non supportato"})),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
for (i, cell) in record.iter().enumerate() {
|
for (i, cell) in record.iter().enumerate() {
|
||||||
if cell == params.classe {
|
if cell == params.classe {
|
||||||
for (day, range) in header.weekdays.clone() {
|
for (day, range) in header.weekdays.clone() {
|
||||||
|
Loading…
Reference in New Issue
Block a user