use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Error {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
impl Error {
pub fn new(code: i32, message: String) -> Self {
Error {
code,
message,
data: None,
}
}
pub fn with_data(code: i32, message: String, data: Value) -> Self {
Error {
code,
message,
data: Some(data),
}
}
pub fn parse_error() -> Self {
Error::new(-32700, "Parse error".to_string())
}
pub fn invalid_request() -> Self {
Error::new(-32600, "Invalid Request".to_string())
}
pub fn method_not_found() -> Self {
Error::new(-32601, "Method not found".to_string())
}
pub fn invalid_params() -> Self {
Error::new(-32602, "Invalid params".to_string())
}
pub fn internal_error() -> Self {
Error::new(-32603, "Internal error".to_string())
}
pub fn server_error(code: i32, message: String) -> Self {
if code >= -32099 && code <= -32000 {
Error::new(code, message)
} else {
Error::internal_error()
}
}
}