use super::ErrorData;
use crate::jwt::decode::error::DecodingError;
pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
pub enum ValidationError {
#[error("Topic decoding failed: {0}")]
TopicDecoding(DecodingError),
#[error("Subscription ID decoding failed: {0}")]
SubscriptionIdDecoding(DecodingError),
#[error("Invalid request ID")]
RequestId,
#[error("Invalid JSON RPC version")]
JsonRpcVersion,
#[error("The batch contains too many items ({actual}). Maximum number of items is {limit}")]
BatchLimitExceeded { limit: usize, actual: usize },
#[error("The batch contains no items")]
BatchEmpty,
}
#[derive(Debug, thiserror::Error)]
pub enum GenericError {
#[error("Authorization error: {0}")]
Authorization(BoxError),
#[error("Too many requests")]
TooManyRequests,
#[error("Request validation error: {0}")]
Validation(#[from] ValidationError),
#[error("Serialization failed: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Unsupported request method")]
RequestMethod,
#[error("Failed to process request: {0}")]
Request(BoxError),
#[error("Internal error: {0}")]
Other(BoxError),
}
impl GenericError {
pub fn code(&self) -> i32 {
match self {
Self::Authorization(_) => 3000,
Self::TooManyRequests => 3001,
Self::Serialization(_) => -32700,
Self::Validation(_) => -32602,
Self::RequestMethod => -32601,
Self::Request(_) => -32000,
Self::Other(_) => -32603,
}
}
}
impl<T> From<T> for ErrorData
where
T: Into<GenericError>,
{
fn from(value: T) -> Self {
let value = value.into();
ErrorData { code: value.code(), message: value.to_string(), data: None }
}
}