use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case")]
pub struct ErrorPayload {
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
pub error_type: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub stack_trace: Vec<StackFrame>,
#[serde(skip_serializing_if = "Option::is_none")]
pub inner: Option<Box<ErrorPayload>>,
#[serde(default, skip_serializing_if = "Map::is_empty")]
pub data: Map<String, Value>,
}
impl ErrorPayload {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: Some(message.into()),
..Self::default()
}
}
pub fn with_type(mut self, error_type: impl Into<String>) -> Self {
self.error_type = Some(error_type.into());
self
}
pub fn with_stack_trace(mut self, stack_trace: Vec<StackFrame>) -> Self {
self.stack_trace = stack_trace;
self
}
pub fn with_inner(mut self, inner: ErrorPayload) -> Self {
self.inner = Some(Box::new(inner));
self
}
pub fn with_data(mut self, key: impl Into<String>, value: Value) -> Self {
self.data.insert(key.into(), value);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "snake_case")]
pub struct StackFrame {
#[serde(skip_serializing_if = "Option::is_none")]
pub method: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub file_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub line_number: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub column: Option<u32>,
}
impl StackFrame {
pub fn new(method: impl Into<String>) -> Self {
Self {
method: Some(method.into()),
..Self::default()
}
}
pub fn with_file(mut self, file_name: impl Into<String>, line_number: u32) -> Self {
self.file_name = Some(file_name.into());
self.line_number = Some(line_number);
self
}
pub fn with_column(mut self, column: u32) -> Self {
self.column = Some(column);
self
}
}