codemp/src/errors.rs

65 lines
2.1 KiB
Rust
Raw Normal View History

//! ### Errors
//! Contains the crate's error types.
/// An error returned by the server as response to a request.
///
/// This currently wraps an [http code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status),
/// returned as procedure status.
#[derive(Debug, thiserror::Error)]
2024-09-11 17:50:05 +02:00
#[error("server rejected procedure with error code: {0:?}")]
pub struct RemoteError(#[from] tonic::Status);
/// Wraps [std::result::Result] with a [RemoteError].
pub type RemoteResult<T> = std::result::Result<T, RemoteError>;
/// An error that may occur when processing requests that require new connections.
2024-08-06 23:00:04 +02:00
#[derive(Debug, thiserror::Error)]
pub enum ConnectionError {
/// Underlying [`tonic::transport::Error`].
2024-09-11 17:50:05 +02:00
#[error("transport error: {0:?}")]
Transport(#[from] tonic::transport::Error),
/// Error from the remote server, see [`RemoteError`].
2024-09-11 17:50:05 +02:00
#[error("server rejected connection attempt: {0:?}")]
Remote(#[from] RemoteError),
}
impl From<tonic::Status> for ConnectionError {
fn from(value: tonic::Status) -> Self {
Self::Remote(RemoteError(value))
}
}
/// Wraps [std::result::Result] with a [ConnectionError].
pub type ConnectionResult<T> = std::result::Result<T, ConnectionError>;
/// An error that may occur when an [`crate::api::Controller`] attempts to
/// perform an illegal operation.
#[derive(Debug, thiserror::Error)]
pub enum ControllerError {
/// Error occurred because the underlying controller worker is already stopped.
#[error("worker is already stopped")]
Stopped,
/// Error occurred because the underlying controller worker stopped before
/// fulfilling the request, without rejecting it first.
#[error("worker stopped before completing requested operation")]
Unfulfilled,
}
impl<T> From<tokio::sync::mpsc::error::SendError<T>> for ControllerError {
fn from(_: tokio::sync::mpsc::error::SendError<T>) -> Self {
Self::Stopped
}
}
impl From<tokio::sync::oneshot::error::RecvError> for ControllerError {
fn from(_: tokio::sync::oneshot::error::RecvError) -> Self {
Self::Unfulfilled
}
}
/// Wraps [std::result::Result] with a [ControllerError].
pub type ControllerResult<T> = std::result::Result<T, ControllerError>;