1- use std:: fmt:: Display ;
2-
3- use crate :: model:: ErrorData ;
1+ use std:: { borrow:: Cow , fmt:: Display } ;
42
3+ use crate :: ServiceError ;
4+ pub use crate :: model:: ErrorData ;
5+ #[ deprecated(
6+ note = "Use `rmcp::ErrorData` instead, `rmcp::ErrorData` could become `RmcpError` in the future."
7+ ) ]
58pub type Error = ErrorData ;
6-
79impl Display for ErrorData {
810 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
911 write ! ( f, "{}: {}" , self . code. 0 , self . message) ?;
@@ -15,3 +17,40 @@ impl Display for ErrorData {
1517}
1618
1719impl std:: error:: Error for ErrorData { }
20+
21+ /// This is an unified error type for the errors could be returned by the service.
22+ #[ derive( Debug , thiserror:: Error ) ]
23+ pub enum RmcpError {
24+ #[ error( "Service error: {0}" ) ]
25+ Service ( #[ from] ServiceError ) ,
26+ #[ cfg( feature = "client" ) ]
27+ #[ error( "Client initialization error: {0}" ) ]
28+ ClientInitialize ( #[ from] crate :: service:: ClientInitializeError ) ,
29+ #[ cfg( feature = "server" ) ]
30+ #[ error( "Server initialization error: {0}" ) ]
31+ ServerInitialize ( #[ from] crate :: service:: ServerInitializeError ) ,
32+ #[ error( "Runtime error: {0}" ) ]
33+ Runtime ( #[ from] tokio:: task:: JoinError ) ,
34+ #[ error( "Transport creation error: {error}" ) ]
35+ // TODO: Maybe we can introduce something like `TryIntoTransport` to auto wrap transport type,
36+ // but it could be an breaking change, so we could do it in the future.
37+ TransportCreation {
38+ into_transport_type_name : Cow < ' static , str > ,
39+ into_transport_type_id : std:: any:: TypeId ,
40+ #[ source]
41+ error : Box < dyn std:: error:: Error + Send + Sync > ,
42+ } ,
43+ // and cancellation shouldn't be an error?
44+ }
45+
46+ impl RmcpError {
47+ pub fn transport_creation < T : ' static > (
48+ error : impl Into < Box < dyn std:: error:: Error + Send + Sync > > ,
49+ ) -> Self {
50+ RmcpError :: TransportCreation {
51+ into_transport_type_id : std:: any:: TypeId :: of :: < T > ( ) ,
52+ into_transport_type_name : std:: any:: type_name :: < T > ( ) . into ( ) ,
53+ error : error. into ( ) ,
54+ }
55+ }
56+ }
0 commit comments