use std::{fmt::Display, str};
use schemars::{JsonSchema, Schema};
use serde::{Deserialize, Serialize};
use crate::IntoOption;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[non_exhaustive]
pub struct Error {
pub code: ErrorCode,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<serde_json::Value>,
}
impl Error {
#[must_use]
pub fn new(code: i32, message: impl Into<String>) -> Self {
Error {
code: code.into(),
message: message.into(),
data: None,
}
}
#[must_use]
pub fn data(mut self, data: impl IntoOption<serde_json::Value>) -> Self {
self.data = data.into_option();
self
}
#[must_use]
pub fn parse_error() -> Self {
ErrorCode::ParseError.into()
}
#[must_use]
pub fn invalid_request() -> Self {
ErrorCode::InvalidRequest.into()
}
#[must_use]
pub fn method_not_found() -> Self {
ErrorCode::MethodNotFound.into()
}
#[must_use]
pub fn invalid_params() -> Self {
ErrorCode::InvalidParams.into()
}
#[must_use]
pub fn internal_error() -> Self {
ErrorCode::InternalError.into()
}
#[cfg(feature = "unstable_cancel_request")]
#[must_use]
pub fn request_cancelled() -> Self {
ErrorCode::RequestCancelled.into()
}
#[must_use]
pub fn auth_required() -> Self {
ErrorCode::AuthRequired.into()
}
#[cfg(feature = "unstable_elicitation")]
#[must_use]
pub fn url_elicitation_required() -> Self {
ErrorCode::UrlElicitationRequired.into()
}
#[must_use]
pub fn resource_not_found(uri: Option<String>) -> Self {
let err: Self = ErrorCode::ResourceNotFound.into();
if let Some(uri) = uri {
err.data(serde_json::json!({ "uri": uri }))
} else {
err
}
}
#[must_use]
pub fn into_internal_error(err: impl std::error::Error) -> Self {
Error::internal_error().data(err.to_string())
}
}
#[derive(Clone, Copy, Deserialize, Eq, JsonSchema, PartialEq, Serialize, strum::Display)]
#[cfg_attr(test, derive(strum::EnumIter))]
#[serde(from = "i32", into = "i32")]
#[schemars(!from, !into)]
#[non_exhaustive]
pub enum ErrorCode {
#[schemars(transform = error_code_transform)]
#[strum(to_string = "Parse error")]
ParseError, #[schemars(transform = error_code_transform)]
#[strum(to_string = "Invalid request")]
InvalidRequest, #[schemars(transform = error_code_transform)]
#[strum(to_string = "Method not found")]
MethodNotFound, #[schemars(transform = error_code_transform)]
#[strum(to_string = "Invalid params")]
InvalidParams, #[schemars(transform = error_code_transform)]
#[strum(to_string = "Internal error")]
InternalError, #[cfg(feature = "unstable_cancel_request")]
#[schemars(transform = error_code_transform)]
#[strum(to_string = "Request cancelled")]
RequestCancelled,
#[schemars(transform = error_code_transform)]
#[strum(to_string = "Authentication required")]
AuthRequired, #[schemars(transform = error_code_transform)]
#[strum(to_string = "Resource not found")]
ResourceNotFound, #[cfg(feature = "unstable_elicitation")]
#[schemars(transform = error_code_transform)]
#[strum(to_string = "URL elicitation required")]
UrlElicitationRequired,
#[schemars(untagged)]
#[strum(to_string = "Unknown error")]
Other(i32),
}
impl From<i32> for ErrorCode {
fn from(value: i32) -> Self {
match value {
-32700 => ErrorCode::ParseError,
-32600 => ErrorCode::InvalidRequest,
-32601 => ErrorCode::MethodNotFound,
-32602 => ErrorCode::InvalidParams,
-32603 => ErrorCode::InternalError,
#[cfg(feature = "unstable_cancel_request")]
-32800 => ErrorCode::RequestCancelled,
-32000 => ErrorCode::AuthRequired,
-32002 => ErrorCode::ResourceNotFound,
#[cfg(feature = "unstable_elicitation")]
-32042 => ErrorCode::UrlElicitationRequired,
_ => ErrorCode::Other(value),
}
}
}
impl From<ErrorCode> for i32 {
fn from(value: ErrorCode) -> Self {
match value {
ErrorCode::ParseError => -32700,
ErrorCode::InvalidRequest => -32600,
ErrorCode::MethodNotFound => -32601,
ErrorCode::InvalidParams => -32602,
ErrorCode::InternalError => -32603,
#[cfg(feature = "unstable_cancel_request")]
ErrorCode::RequestCancelled => -32800,
ErrorCode::AuthRequired => -32000,
ErrorCode::ResourceNotFound => -32002,
#[cfg(feature = "unstable_elicitation")]
ErrorCode::UrlElicitationRequired => -32042,
ErrorCode::Other(value) => value,
}
}
}
impl std::fmt::Debug for ErrorCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {self}", i32::from(*self))
}
}
fn error_code_transform(schema: &mut Schema) {
let name = schema
.get("const")
.expect("Unexpected schema for ErrorCode")
.as_str()
.expect("unexpected type for schema");
let code = match name {
"ParseError" => ErrorCode::ParseError,
"InvalidRequest" => ErrorCode::InvalidRequest,
"MethodNotFound" => ErrorCode::MethodNotFound,
"InvalidParams" => ErrorCode::InvalidParams,
"InternalError" => ErrorCode::InternalError,
#[cfg(feature = "unstable_cancel_request")]
"RequestCancelled" => ErrorCode::RequestCancelled,
"AuthRequired" => ErrorCode::AuthRequired,
"ResourceNotFound" => ErrorCode::ResourceNotFound,
#[cfg(feature = "unstable_elicitation")]
"UrlElicitationRequired" => ErrorCode::UrlElicitationRequired,
_ => panic!("Unexpected error code name {name}"),
};
let mut description = schema
.get("description")
.expect("Missing description")
.as_str()
.expect("Unexpected type for description")
.to_owned();
schema.insert("title".into(), code.to_string().into());
description.insert_str(0, &format!("**{code}**: "));
schema.insert("description".into(), description.into());
schema.insert("const".into(), i32::from(code).into());
schema.insert("type".into(), "integer".into());
schema.insert("format".into(), "int32".into());
}
impl From<ErrorCode> for Error {
fn from(error_code: ErrorCode) -> Self {
Error::new(error_code.into(), error_code.to_string())
}
}
impl std::error::Error for Error {}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.message.is_empty() {
write!(f, "{}", i32::from(self.code))?;
} else {
write!(f, "{}", self.message)?;
}
if let Some(data) = &self.data {
let pretty = serde_json::to_string_pretty(data).unwrap_or_else(|_| data.to_string());
write!(f, ": {pretty}")?;
}
Ok(())
}
}
impl From<anyhow::Error> for Error {
fn from(error: anyhow::Error) -> Self {
match error.downcast::<Self>() {
Ok(error) => error,
Err(error) => Error::into_internal_error(&*error),
}
}
}
impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Self {
Error::invalid_params().data(error.to_string())
}
}
#[cfg(test)]
mod tests {
use strum::IntoEnumIterator;
use super::*;
#[test]
fn serialize_error_code() {
assert_eq!(
serde_json::from_value::<ErrorCode>(serde_json::json!(-32700)).unwrap(),
ErrorCode::ParseError
);
assert_eq!(
serde_json::to_value(ErrorCode::ParseError).unwrap(),
serde_json::json!(-32700)
);
assert_eq!(
serde_json::from_value::<ErrorCode>(serde_json::json!(1)).unwrap(),
ErrorCode::Other(1)
);
assert_eq!(
serde_json::to_value(ErrorCode::Other(1)).unwrap(),
serde_json::json!(1)
);
}
#[test]
fn serialize_error_code_equality() {
let _schema = schemars::schema_for!(ErrorCode);
for error in ErrorCode::iter() {
assert_eq!(
error,
serde_json::from_value(serde_json::to_value(error).unwrap()).unwrap()
);
}
}
}