use std::{error, fmt};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::types::Value;
#[derive(Debug, PartialEq, Clone)]
pub enum ErrorCode {
ParseError,
InvalidRequest,
MethodNotFound,
InvalidParams,
InternalError,
ServerError(i64),
}
impl ErrorCode {
pub fn code(&self) -> i64 {
match *self {
ErrorCode::ParseError => -32700,
ErrorCode::InvalidRequest => -32600,
ErrorCode::MethodNotFound => -32601,
ErrorCode::InvalidParams => -32602,
ErrorCode::InternalError => -32603,
ErrorCode::ServerError(code) => code,
}
}
pub fn description(&self) -> String {
let desc = match *self {
ErrorCode::ParseError => "Parse error",
ErrorCode::InvalidRequest => "Invalid request",
ErrorCode::MethodNotFound => "Method not found",
ErrorCode::InvalidParams => "Invalid params",
ErrorCode::InternalError => "Internal error",
ErrorCode::ServerError(_) => "Server error",
};
desc.to_string()
}
}
impl From<i64> for ErrorCode {
fn from(code: i64) -> Self {
match code {
-32700 => ErrorCode::ParseError,
-32600 => ErrorCode::InvalidRequest,
-32601 => ErrorCode::MethodNotFound,
-32602 => ErrorCode::InvalidParams,
-32603 => ErrorCode::InternalError,
code => ErrorCode::ServerError(code),
}
}
}
impl Serialize for ErrorCode {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_i64(self.code())
}
}
impl<'de> Deserialize<'de> for ErrorCode {
fn deserialize<D>(deserializer: D) -> Result<ErrorCode, D::Error>
where
D: Deserializer<'de>,
{
let code: i64 = Deserialize::deserialize(deserializer)?;
Ok(ErrorCode::from(code))
}
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct Error {
pub code: ErrorCode,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
impl Error {
pub fn new(code: ErrorCode) -> Self {
Error {
message: code.description(),
code,
data: None,
}
}
pub fn parse_error() -> Self {
Self::new(ErrorCode::ParseError)
}
pub fn invalid_request() -> Self {
Self::new(ErrorCode::InvalidRequest)
}
pub fn method_not_found() -> Self {
Self::new(ErrorCode::MethodNotFound)
}
pub fn invalid_params<M>(message: M) -> Self
where
M: Into<String>,
{
Error {
code: ErrorCode::InvalidParams,
message: message.into(),
data: None,
}
}
pub fn invalid_params_with_details<M, T>(message: M, details: T) -> Error
where
M: Into<String>,
T: fmt::Debug,
{
Error {
code: ErrorCode::InvalidParams,
message: format!("Invalid parameters: {}", message.into()),
data: Some(Value::String(format!("{:?}", details))),
}
}
pub fn internal_error() -> Self {
Self::new(ErrorCode::InternalError)
}
pub fn invalid_version() -> Self {
Error {
code: ErrorCode::InvalidRequest,
message: "Unsupported JSON-RPC protocol version".to_owned(),
data: None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}: {}", self.code.description(), self.message)
}
}
impl error::Error for Error {}