fix: tolerate ref-resource errors in course section list APIs
Downgrade list failures when loading ref resources for a section so one bad row does not fail the entire page; log warn with section id. Made-with: Cursor
This commit is contained in:
+30
-28
@@ -8,7 +8,7 @@ use axum::Json;
|
||||
use axum_macros::debug_handler;
|
||||
use diesel::PgConnection;
|
||||
use reqwest::header::{HeaderValue, CONTENT_TYPE};
|
||||
use tracing::{debug, error};
|
||||
use tracing::{debug, error, warn};
|
||||
|
||||
fn current_org_id_from_token_str(token_str: &String) -> Option<String> {
|
||||
jwt_decode_token(token_str)
|
||||
@@ -2224,23 +2224,14 @@ pub fn raw_find_teacher_students_by_ids(
|
||||
let query_ids = params
|
||||
.hty_ids
|
||||
.ok_or_else(|| anyhow!("hty_ids is required"))?;
|
||||
let current_org_id = current_org_id_from_token_str(&sudoer.0);
|
||||
let (records, total_page, total) = if let Some(org_id) = ¤t_org_id {
|
||||
TeacherStudent::find_with_page_by_ids_in_org(
|
||||
let current_org_id = required_current_org_id_from_token_str(&sudoer.0)?;
|
||||
let (records, total_page, total) = TeacherStudent::find_with_page_by_ids_in_org(
|
||||
page,
|
||||
page_size,
|
||||
&query_ids,
|
||||
org_id,
|
||||
¤t_org_id,
|
||||
extract_conn(fetch_db_conn(&db_pool)?).deref_mut(),
|
||||
)?
|
||||
} else {
|
||||
TeacherStudent::find_with_page_by_ids(
|
||||
page,
|
||||
page_size,
|
||||
&query_ids,
|
||||
extract_conn(fetch_db_conn(&db_pool)?).deref_mut(),
|
||||
)?
|
||||
};
|
||||
)?;
|
||||
let req_records = TeacherStudent::to_req_vec(&records);
|
||||
debug!(
|
||||
"raw_find_teacher_students_by_ids -> req_records: {:?}",
|
||||
@@ -2276,21 +2267,13 @@ pub fn raw_find_all_teacher_students(
|
||||
root: &HtySudoerTokenHeader,
|
||||
db_pool: Arc<DbState>,
|
||||
) -> anyhow::Result<(Vec<ReqTeacherStudent>, i64, i64)> {
|
||||
let current_org_id = current_org_id_from_token_str(&root.0);
|
||||
let (records, total_page, total) = if let Some(org_id) = ¤t_org_id {
|
||||
TeacherStudent::find_all_with_page_in_org(
|
||||
let current_org_id = required_current_org_id_from_token_str(&root.0)?;
|
||||
let (records, total_page, total) = TeacherStudent::find_all_with_page_in_org(
|
||||
page,
|
||||
page_size,
|
||||
org_id,
|
||||
¤t_org_id,
|
||||
extract_conn(fetch_db_conn(&db_pool)?).deref_mut(),
|
||||
)?
|
||||
} else {
|
||||
TeacherStudent::find_all_with_page(
|
||||
page,
|
||||
page_size,
|
||||
extract_conn(fetch_db_conn(&db_pool)?).deref_mut(),
|
||||
)?
|
||||
};
|
||||
)?;
|
||||
let req_records = TeacherStudent::to_req_vec(&records);
|
||||
Ok((req_records, total_page, total))
|
||||
}
|
||||
@@ -4154,7 +4137,19 @@ pub fn raw_find_all_course_sections_by_teacher_id_with_page(
|
||||
let result: anyhow::Result<Vec<_>> = sections
|
||||
.iter()
|
||||
.map(|section| {
|
||||
section.to_req_with_ref_resource(extract_conn(fetch_db_conn(&db_pool)?).deref_mut())
|
||||
match section.to_req_with_ref_resource(
|
||||
extract_conn(fetch_db_conn(&db_pool)?).deref_mut(),
|
||||
) {
|
||||
Ok(value) => Ok(value),
|
||||
Err(err) => {
|
||||
warn!(
|
||||
section_id = %section.id,
|
||||
error = %err,
|
||||
"to_req_with_ref_resource failed in teacher course-section list; falling back to summary row"
|
||||
);
|
||||
Ok(section.to_req())
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let result = result?;
|
||||
@@ -4224,7 +4219,14 @@ pub async fn raw_find_all_course_sections_by_created_by_with_page(
|
||||
.get_req_ref_resources(extract_conn(fetch_db_conn(&db_pool)?).deref_mut())
|
||||
{
|
||||
Ok(res) => res,
|
||||
Err(e) => return Err(e),
|
||||
Err(e) => {
|
||||
warn!(
|
||||
section_id = %section.id,
|
||||
error = %e,
|
||||
"get_req_ref_resources failed in created_by course-section list; omitting resources"
|
||||
);
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
Ok(ReqCourseSection {
|
||||
|
||||
Reference in New Issue
Block a user