|
| 1 | +use std::error::Error; |
| 2 | + |
| 3 | +use crate::error::into_ok::into_ok; |
| 4 | +use crate::error::Fatal; |
| 5 | +use crate::error::Infallible; |
| 6 | +use crate::error::RPCError; |
| 7 | +use crate::error::RaftError; |
| 8 | +use crate::error::StreamingError; |
| 9 | +use crate::error::Unreachable; |
| 10 | +use crate::RaftTypeConfig; |
| 11 | + |
| 12 | +/// Simplifies error handling by extracting the inner error from a composite error. |
| 13 | +/// For example, converting `Result<R, CompositeError>` |
| 14 | +/// to `Result<Result<R, Self::InnerError>, OuterError>`, |
| 15 | +/// where `SomeCompositeError` is a composite of `Self::InnerError` and `OuterError`. |
| 16 | +pub trait DecomposeResult<C, R, OuterError> |
| 17 | +where C: RaftTypeConfig |
| 18 | +{ |
| 19 | + type InnerError; |
| 20 | + |
| 21 | + fn decompose(self) -> Result<Result<R, Self::InnerError>, OuterError>; |
| 22 | + |
| 23 | + /// Convert `Result<R, CompositeErr>` |
| 24 | + /// to `Result<R, E>`, |
| 25 | + /// if `Self::InnerError` is a infallible type. |
| 26 | + fn decompose_infallible(self) -> Result<R, OuterError> |
| 27 | + where |
| 28 | + Self::InnerError: Into<Infallible>, |
| 29 | + Self: Sized, |
| 30 | + { |
| 31 | + self.decompose().map(into_ok) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl<C, R, E> DecomposeResult<C, R, RaftError<C::NodeId>> for Result<R, RaftError<C::NodeId, E>> |
| 36 | +where C: RaftTypeConfig |
| 37 | +{ |
| 38 | + type InnerError = E; |
| 39 | + |
| 40 | + fn decompose(self) -> Result<Result<R, E>, RaftError<C::NodeId>> { |
| 41 | + match self { |
| 42 | + Ok(r) => Ok(Ok(r)), |
| 43 | + Err(e) => match e { |
| 44 | + RaftError::APIError(e) => Ok(Err(e)), |
| 45 | + RaftError::Fatal(e) => Err(RaftError::Fatal(e)), |
| 46 | + }, |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl<C, R, E> DecomposeResult<C, R, RPCError<C::NodeId, C::Node>> |
| 52 | + for Result<R, RPCError<C::NodeId, C::Node, RaftError<C::NodeId, E>>> |
| 53 | +where |
| 54 | + C: RaftTypeConfig, |
| 55 | + E: Error, |
| 56 | +{ |
| 57 | + type InnerError = E; |
| 58 | + |
| 59 | + /// `RaftError::Fatal` is considered as `RPCError::Unreachable`. |
| 60 | + fn decompose(self) -> Result<Result<R, E>, RPCError<C::NodeId, C::Node>> { |
| 61 | + match self { |
| 62 | + Ok(r) => Ok(Ok(r)), |
| 63 | + Err(e) => match e { |
| 64 | + RPCError::Timeout(e) => Err(RPCError::Timeout(e)), |
| 65 | + RPCError::Unreachable(e) => Err(RPCError::Unreachable(e)), |
| 66 | + RPCError::PayloadTooLarge(e) => Err(RPCError::PayloadTooLarge(e)), |
| 67 | + RPCError::Network(e) => Err(RPCError::Network(e)), |
| 68 | + RPCError::RemoteError(e) => match e.source { |
| 69 | + RaftError::APIError(e) => Ok(Err(e)), |
| 70 | + RaftError::Fatal(e) => Err(RPCError::Unreachable(Unreachable::new(&e))), |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl<C, R> DecomposeResult<C, R, StreamingError<C>> for Result<R, StreamingError<C, Fatal<C::NodeId>>> |
| 78 | +where C: RaftTypeConfig |
| 79 | +{ |
| 80 | + type InnerError = Infallible; |
| 81 | + |
| 82 | + /// `Fatal` is considered as `RPCError::Unreachable`. |
| 83 | + fn decompose(self) -> Result<Result<R, Self::InnerError>, StreamingError<C>> { |
| 84 | + match self { |
| 85 | + Ok(r) => Ok(Ok(r)), |
| 86 | + Err(e) => match e { |
| 87 | + StreamingError::Closed(e) => Err(StreamingError::Closed(e)), |
| 88 | + StreamingError::StorageError(e) => Err(StreamingError::StorageError(e)), |
| 89 | + StreamingError::Timeout(e) => Err(StreamingError::Timeout(e)), |
| 90 | + StreamingError::Unreachable(e) => Err(StreamingError::Unreachable(e)), |
| 91 | + StreamingError::Network(e) => Err(StreamingError::Network(e)), |
| 92 | + StreamingError::RemoteError(e) => Err(StreamingError::Unreachable(Unreachable::new(&e.source))), |
| 93 | + }, |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments