use http::StatusCode;
use serde::Serialize;
use serde_json::Value;
use crate::response::{IntoResponse, Status};
#[derive(Serialize)]
pub struct Error {
status: Status,
message: String,
#[serde(skip_serializing_if = "Option::is_none")]
code: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
data: Option<Value>,
#[serde(skip)]
http_status: StatusCode,
}
impl Error {
pub fn new(
message: impl Into<String>,
code: Option<usize>,
data: Option<Value>,
http_status: StatusCode,
) -> Self {
Self {
status: Status::Error,
message: message.into(),
code,
data,
http_status,
}
}
pub fn status(&self) -> &Status {
&self.status
}
pub fn message(&self) -> &str {
&self.message
}
pub fn code(&self) -> Option<usize> {
self.code
}
pub fn data(&self) -> Option<&Value> {
self.data.as_ref()
}
}
impl IntoResponse for Error {
fn http_status(&self) -> StatusCode {
self.http_status
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::{from_slice, json};
fn to_json<R: IntoResponse + Serialize>(response: R) -> Value {
let http_response = response
.into_response()
.expect("serialisation should succeed");
from_slice(http_response.body()).expect("body should be valid JSON")
}
#[test]
fn requires_only_message() {
let response = Error::new(
"playback service unreachable",
None,
None,
StatusCode::INTERNAL_SERVER_ERROR,
);
let body = to_json(response);
assert_eq!(body["status"], "error");
assert_eq!(body["message"], "playback service unreachable");
assert!(body.get("code").is_none());
assert!(body.get("data").is_none());
}
#[test]
fn includes_code_when_present() {
let response = Error::new(
"rate limited",
Some(67),
None,
StatusCode::TOO_MANY_REQUESTS,
);
assert_eq!(to_json(response)["code"], 67);
}
#[test]
fn includes_data_when_present() {
let response = Error::new(
"upstream failure",
None,
Some(json!({"service": "playback_api", "trace_id": "42736"})),
StatusCode::BAD_GATEWAY,
);
assert_eq!(to_json(response)["data"]["trace_id"], "42736");
}
#[test]
fn status_is_error() {
let response = Error::new(
"playback service unreachable",
None,
None,
StatusCode::INTERNAL_SERVER_ERROR,
);
assert_eq!(response.status(), &Status::Error);
}
#[test]
fn message_returns_constructed_value() {
let response = Error::new(
"playback service unreachable",
None,
None,
StatusCode::INTERNAL_SERVER_ERROR,
);
assert_eq!(response.message(), "playback service unreachable");
}
#[test]
fn code_returns_constructed_value() {
let response = Error::new(
"rate limited",
Some(67),
None,
StatusCode::TOO_MANY_REQUESTS,
);
assert_eq!(response.code(), Some(67));
}
#[test]
fn code_returns_none_when_absent() {
let response = Error::new(
"playback service unreachable",
None,
None,
StatusCode::INTERNAL_SERVER_ERROR,
);
assert_eq!(response.code(), None);
}
#[test]
fn data_returns_constructed_value() {
let response = Error::new(
"upstream failure",
None,
Some(json!({"service": "playback_api", "trace_id": "42736"})),
StatusCode::BAD_GATEWAY,
);
assert_eq!(
response.data(),
Some(&json!({"service": "playback_api", "trace_id": "42736"}))
);
}
#[test]
fn data_returns_none_when_absent() {
let response = Error::new(
"playback service unreachable",
None,
None,
StatusCode::INTERNAL_SERVER_ERROR,
);
assert_eq!(response.data(), None);
}
}