-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy patherror.rs
More file actions
90 lines (84 loc) · 2.96 KB
/
Copy patherror.rs
File metadata and controls
90 lines (84 loc) · 2.96 KB
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
use axum::{
http::{
header::{HeaderValue, RETRY_AFTER},
HeaderMap, StatusCode,
},
response::{IntoResponse, Response},
Json,
};
use serde::Serialize;
use crate::types::ValidationErrorResponse;
/// Error type used by the audit `event` module (CT-39).
#[derive(Debug, thiserror::Error)]
pub enum AuditError {
#[error("event serialization failed: {0}")]
SerializationError(String),
}
/// Convenience alias used across the audit module.
pub type Result<T> = std::result::Result<T, AuditError>;
/// Structured 503 response payload returned when upstream Horizon is
/// unreachable after the retry budget is exhausted or the circuit breaker
/// is open. Stable JSON contract: `{ status: "indeterminate", message, attempt_count }`.
#[derive(Debug, Serialize)]
pub struct IndeterminateResponse {
/// Always literal string "indeterminate" -- kept stable for client parsing.
pub status: &'static str,
/// Human-readable message suitable for surfacing to operators / logs.
pub message: String,
/// How many retry attempts we made before giving up.
pub attempt_count: u32,
}
#[derive(Debug)]
pub enum AppError {
Validation(String),
NotFound(String),
Internal(String),
BadGateway(String),
/// Verification could not be completed because Horizon was unreachable
/// after exhausting the configured retry budget, or because the circuit
/// breaker is currently open. Maps to HTTP 503 with a `Retry-After`
/// header so the caller can back off cleanly.
Indeterminate {
message: String,
attempt_count: u32,
},
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
match self {
AppError::Validation(msg) => (
StatusCode::BAD_REQUEST,
Json(ValidationErrorResponse { error: msg }),
)
.into_response(),
AppError::NotFound(msg) => (
StatusCode::NOT_FOUND,
Json(ValidationErrorResponse { error: msg }),
)
.into_response(),
AppError::Internal(msg) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(ValidationErrorResponse { error: msg }),
)
.into_response(),
AppError::BadGateway(msg) => (
StatusCode::BAD_GATEWAY,
Json(ValidationErrorResponse { error: msg }),
)
.into_response(),
AppError::Indeterminate {
message,
attempt_count,
} => {
let body = IndeterminateResponse {
status: "indeterminate",
message,
attempt_count,
};
let mut headers = HeaderMap::new();
headers.insert(RETRY_AFTER, HeaderValue::from_static("30"));
(StatusCode::SERVICE_UNAVAILABLE, headers, Json(body)).into_response()
}
}
}
}