forked from StellarRouter/StellarRouter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
75 lines (64 loc) · 2.13 KB
/
Copy patherror.rs
File metadata and controls
75 lines (64 loc) · 2.13 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
//! Shared HTTP error response types.
//!
//! These types are used across both the API server and the metrics exporter
//! to produce consistent JSON error payloads.
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use serde::Serialize;
// ── Generic error response ────────────────────────────────────────────────────
/// A generic JSON error response body.
///
/// Serialised as `{"error": "<message>"}`.
#[derive(Debug, Clone, Serialize)]
pub struct ErrorResponse {
pub error: String,
}
impl ErrorResponse {
/// Create a new [`ErrorResponse`] with the given message.
pub fn new(message: impl Into<String>) -> Self {
Self {
error: message.into(),
}
}
}
// ── Validation error ──────────────────────────────────────────────────────────
/// A structured validation error returned as JSON with HTTP 422.
///
/// Serialised as `{"error": "validation_error", "message": "<detail>"}`.
#[derive(Debug, Clone, Serialize)]
pub struct ValidationError {
pub error: &'static str,
pub message: String,
}
impl ValidationError {
/// Create a new [`ValidationError`] with the given detail message.
pub fn new(message: impl Into<String>) -> Self {
Self {
error: "validation_error",
message: message.into(),
}
}
}
impl IntoResponse for ValidationError {
fn into_response(self) -> Response {
(StatusCode::UNPROCESSABLE_ENTITY, Json(self)).into_response()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_response_contains_message() {
let e = ErrorResponse::new("something went wrong");
assert_eq!(e.error, "something went wrong");
}
#[test]
fn validation_error_has_fixed_kind() {
let e = ValidationError::new("field is required");
assert_eq!(e.error, "validation_error");
assert_eq!(e.message, "field is required");
}
}