feat: unified error type (#308)
* feat: unified error type * fix: use type id to compare transport type
This commit is contained in:
parent
c74b26b3e3
commit
e615300fea
1 changed files with 286 additions and 161 deletions
|
|
@ -1,9 +1,11 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use crate::model::ErrorData;
|
||||
use std::{borrow::Cow, fmt::Display};
|
||||
|
||||
use crate::ServiceError;
|
||||
pub use crate::model::ErrorData;
|
||||
#[deprecated(
|
||||
note = "Use `rmcp::ErrorData` instead, `rmcp::ErrorData` could become `RmcpError` in the future."
|
||||
)]
|
||||
pub type Error = ErrorData;
|
||||
|
||||
impl Display for ErrorData {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}: {}", self.code.0, self.message)?;
|
||||
|
|
@ -15,3 +17,40 @@ impl Display for ErrorData {
|
|||
}
|
||||
|
||||
impl std::error::Error for ErrorData {}
|
||||
|
||||
/// This is an unified error type for the errors could be returned by the service.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum RmcpError {
|
||||
#[error("Service error: {0}")]
|
||||
Service(#[from] ServiceError),
|
||||
#[cfg(feature = "client")]
|
||||
#[error("Client initialization error: {0}")]
|
||||
ClientInitialize(#[from] crate::service::ClientInitializeError),
|
||||
#[cfg(feature = "server")]
|
||||
#[error("Server initialization error: {0}")]
|
||||
ServerInitialize(#[from] crate::service::ServerInitializeError),
|
||||
#[error("Runtime error: {0}")]
|
||||
Runtime(#[from] tokio::task::JoinError),
|
||||
#[error("Transport creation error: {error}")]
|
||||
// TODO: Maybe we can introduce something like `TryIntoTransport` to auto wrap transport type,
|
||||
// but it could be an breaking change, so we could do it in the future.
|
||||
TransportCreation {
|
||||
into_transport_type_name: Cow<'static, str>,
|
||||
into_transport_type_id: std::any::TypeId,
|
||||
#[source]
|
||||
error: Box<dyn std::error::Error + Send + Sync>,
|
||||
},
|
||||
// and cancellation shouldn't be an error?
|
||||
}
|
||||
|
||||
impl RmcpError {
|
||||
pub fn transport_creation<T: 'static>(
|
||||
error: impl Into<Box<dyn std::error::Error + Send + Sync>>,
|
||||
) -> Self {
|
||||
RmcpError::TransportCreation {
|
||||
into_transport_type_id: std::any::TypeId::of::<T>(),
|
||||
into_transport_type_name: std::any::type_name::<T>().into(),
|
||||
error: error.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue