use rustapi_macros::Schema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, Schema)]
pub struct ErrorSchema {
pub error: ErrorBodySchema,
#[serde(skip_serializing_if = "Option::is_none")]
pub request_id: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Schema)]
pub struct ErrorBodySchema {
#[serde(rename = "type")]
pub error_type: String,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub fields: Option<Vec<FieldErrorSchema>>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Schema)]
pub struct FieldErrorSchema {
pub field: String,
pub code: String,
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, Schema)]
pub struct ValidationErrorSchema {
pub error: ValidationErrorBodySchema,
}
#[derive(Debug, Clone, Serialize, Deserialize, Schema)]
pub struct ValidationErrorBodySchema {
#[serde(rename = "type")]
pub error_type: String,
pub message: String,
pub fields: Vec<FieldErrorSchema>,
}
impl ValidationErrorSchema {
pub fn example() -> Self {
Self {
error: ValidationErrorBodySchema {
error_type: "validation_error".to_string(),
message: "Request validation failed".to_string(),
fields: vec![FieldErrorSchema {
field: "email".to_string(),
code: "email".to_string(),
message: "Invalid email format".to_string(),
}],
},
}
}
}
impl ErrorSchema {
pub fn not_found_example() -> Self {
Self {
error: ErrorBodySchema {
error_type: "not_found".to_string(),
message: "Resource not found".to_string(),
fields: None,
},
request_id: None,
}
}
pub fn internal_error_example() -> Self {
Self {
error: ErrorBodySchema {
error_type: "internal_error".to_string(),
message: "An internal error occurred".to_string(),
fields: None,
},
request_id: None,
}
}
pub fn bad_request_example() -> Self {
Self {
error: ErrorBodySchema {
error_type: "bad_request".to_string(),
message: "Invalid request".to_string(),
fields: None,
},
request_id: None,
}
}
}