rpc_router/handler/
handler_error.rs1use serde::{Serialize, Serializer};
2use serde_json::Value;
3use std::any::{Any, TypeId};
4use std::collections::HashMap;
5
6pub type HandlerResult<T> = core::result::Result<T, HandlerError>;
7
8type AnyMap = HashMap<TypeId, Box<dyn Any + Send + Sync>>;
9
10#[derive(Debug)]
11pub struct HandlerError {
12 holder: AnyMap,
13 type_name: &'static str,
14}
15
16impl HandlerError {
17 pub fn new<T>(val: T) -> HandlerError
18 where
19 T: Any + Send + Sync,
20 {
21 let mut holder = AnyMap::with_capacity(1);
22 let type_name = std::any::type_name::<T>();
23 holder.insert(TypeId::of::<T>(), Box::new(val));
24 HandlerError { holder, type_name }
25 }
26}
27
28impl HandlerError {
29 pub fn get<T: Any + Send + Sync>(&self) -> Option<&T> {
32 self.holder
33 .get(&TypeId::of::<T>())
34 .and_then(|boxed_any| boxed_any.downcast_ref::<T>())
35 }
36
37 pub fn remove<T: Any + Send + Sync>(&mut self) -> Option<T> {
39 self.holder.remove(&TypeId::of::<T>()).and_then(|boxed_any| {
40 (boxed_any as Box<dyn Any>).downcast::<T>().ok().map(|boxed| *boxed)
42 })
43 }
44
45 pub fn type_name(&self) -> &'static str {
47 self.type_name
48 }
49}
50
51impl Serialize for HandlerError {
53 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
54 where
55 S: Serializer,
56 {
57 serializer.serialize_str(&format!("RpcHandlerError containing error '{}'", self.type_name))
61 }
62}
63
64pub trait IntoHandlerError
70where
71 Self: Sized + Send + Sync + 'static,
72{
73 fn into_handler_error(self) -> HandlerError {
74 HandlerError::new(self)
75 }
76}
77
78impl IntoHandlerError for HandlerError {
79 fn into_handler_error(self) -> HandlerError {
80 self
81 }
82}
83
84impl IntoHandlerError for String {
85 fn into_handler_error(self) -> HandlerError {
86 HandlerError::new(self)
87 }
88}
89
90impl IntoHandlerError for &'static str {
91 fn into_handler_error(self) -> HandlerError {
92 HandlerError::new(self)
93 }
94}
95
96impl IntoHandlerError for Value {
97 fn into_handler_error(self) -> HandlerError {
98 HandlerError::new(self)
99 }
100}
101
102impl core::fmt::Display for HandlerError {
107 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
108 write!(fmt, "{self:?}")
109 }
110}
111
112impl std::error::Error for HandlerError {}
113
114