forked from
atscan.net/plcbundle-rs
High-performance implementation of plcbundle written in Rust
1// Error handling utilities and response helpers
2
3use axum::{http::StatusCode, response::IntoResponse};
4use serde_json::json;
5
6/// Helper to create a JSON error response
7pub fn json_error(status: StatusCode, message: &str) -> impl IntoResponse {
8 (status, axum::Json(json!({"error": message})))
9}
10
11/// Helper for "not found" errors
12pub fn not_found(message: &str) -> impl IntoResponse {
13 json_error(StatusCode::NOT_FOUND, message)
14}
15
16/// Helper for internal server errors
17pub fn internal_error(message: &str) -> impl IntoResponse {
18 json_error(StatusCode::INTERNAL_SERVER_ERROR, message)
19}
20
21/// Helper for task join errors
22pub fn task_join_error(e: impl std::fmt::Display) -> impl IntoResponse {
23 let msg = format!("Task join error: {}", e);
24 (
25 StatusCode::INTERNAL_SERVER_ERROR,
26 axum::Json(json!({"error": msg})),
27 )
28}
29
30/// Helper for bad request errors
31pub fn bad_request(message: &str) -> impl IntoResponse {
32 json_error(StatusCode::BAD_REQUEST, message)
33}
34
35/// Helper for gone (410) errors
36pub fn gone(message: &str) -> impl IntoResponse {
37 json_error(StatusCode::GONE, message)
38}
39
40/// Check if an error indicates "not found"
41pub fn is_not_found_error(e: &anyhow::Error) -> bool {
42 let msg = e.to_string();
43 msg.contains("not found") || msg.contains("not in index")
44}