use jsonrpsee::{IntoResponse, types::ErrorObjectOwned};
use serde::Serialize;
use serde_json::Value;
pub use jsonrpsee::types::error::ErrorCode;
#[derive(Debug, Clone)]
pub struct RpcError {
pub code: ErrorCode,
pub message: String,
pub data: Option<Value>,
}
impl From<RpcError> for ErrorObjectOwned {
fn from(rpc_error: RpcError) -> Self {
Self::from(&rpc_error)
}
}
impl From<&RpcError> for ErrorObjectOwned {
fn from(rpc_error: &RpcError) -> Self {
Self::owned(
rpc_error.code.code(),
&rpc_error.message,
rpc_error.data.clone(),
)
}
}
impl IntoResponse for RpcError {
type Output = <ErrorObjectOwned as IntoResponse>::Output;
fn into_response(self) -> jsonrpsee::ResponsePayload<'static, Self::Output> {
ErrorObjectOwned::from(self).into_response()
}
}
impl Serialize for RpcError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
ErrorObjectOwned::from(self).serialize(serializer)
}
}