use poem::http::StatusCode;
use poem_openapi::{Object, payload::Json};
#[derive(Object, Debug)]
pub struct ErrorResponse {
pub name: String,
pub message: String,
}
impl ErrorResponse {
pub fn from_code(code: impl Into<String>) -> Self {
let code = code.into();
Self {
message: code.clone(),
name: code,
}
}
}
pub trait IntoErrorResponse {
fn into_error_response(self) -> (StatusCode, Json<ErrorResponse>);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_carry_the_code_in_name_and_message_when_built_from_a_code() {
let body = ErrorResponse::from_code("product.not_found");
assert_eq!(body.name, "product.not_found");
assert_eq!(body.message, "product.not_found");
}
}