fix(schedules): use typed error for pause/resume 404 discrimination

Replace brittle msg.contains("not found") string matching in
post_schedule_pause / post_schedule_resume with a typed
ScheduleNotFoundOrCancelled error that handlers downcast on directly.

pause() and resume() now return Err(ScheduleNotFoundOrCancelled(id).into())
instead of bail!("schedule {id} not found or is cancelled"); handlers call
e.downcast_ref::<ScheduleNotFoundOrCancelled>().is_some() for the 404 branch,
making the discrimination stable even if the error message wording changes.
This commit is contained in:
iris 2026-06-27 13:57:50 +02:00 committed by mara
commit dbd4b7a15c
2 changed files with 28 additions and 12 deletions

View file

@ -13,6 +13,8 @@ use axum::{
use problem_details::ProblemDetails;
use crate::scheduled_prompts::ScheduleNotFoundOrCancelled;
use super::{AppState, error_problem, error_response};
/// `GET /api/schedules` — snapshot of every schedule for the
@ -241,11 +243,10 @@ pub(super) async fn post_schedule_pause(
(StatusCode::OK, "ok").into_response()
}
Err(e) => {
let msg = format!("{e:#}");
if msg.contains("not found") || msg.contains("cancelled") {
(StatusCode::NOT_FOUND, msg).into_response()
if e.downcast_ref::<ScheduleNotFoundOrCancelled>().is_some() {
(StatusCode::NOT_FOUND, format!("{e}")).into_response()
} else {
error_response(&format!("pause schedule {id}: {msg}"))
error_response(&format!("pause schedule {id}: {e:#}"))
}
}
}
@ -264,11 +265,10 @@ pub(super) async fn post_schedule_resume(
(StatusCode::OK, "ok").into_response()
}
Err(e) => {
let msg = format!("{e:#}");
if msg.contains("not found") || msg.contains("cancelled") {
(StatusCode::NOT_FOUND, msg).into_response()
if e.downcast_ref::<ScheduleNotFoundOrCancelled>().is_some() {
(StatusCode::NOT_FOUND, format!("{e}")).into_response()
} else {
error_response(&format!("resume schedule {id}: {msg}"))
error_response(&format!("resume schedule {id}: {e:#}"))
}
}
}