golgi/src/error.rs

137 lines
4.4 KiB
Rust
Raw Permalink Normal View History

//! Custom error type.
2021-12-02 13:12:52 +00:00
2021-12-03 09:37:22 +00:00
use std::io::Error as IoError;
2021-12-30 01:08:55 +00:00
use std::str::Utf8Error;
2021-12-02 13:12:52 +00:00
2021-12-03 09:37:22 +00:00
use base64::DecodeError;
use kuska_handshake::async_std::Error as HandshakeError;
use kuska_ssb::api::Error as ApiError;
use kuska_ssb::feed::Error as FeedError;
use kuska_ssb::rpc::Error as RpcError;
use serde_json::Error as JsonError;
2021-12-02 13:12:52 +00:00
2021-12-03 09:37:22 +00:00
/// A custom error type encapsulating all possible errors for this library.
/// `From` implementations are provided for external error types, allowing
/// the `?` operator to be used on functions which return `Result<_, GolgiError>`.
2021-12-02 13:12:52 +00:00
#[derive(Debug)]
pub enum GolgiError {
2021-12-03 09:37:22 +00:00
/// Failed to decode base64.
DecodeBase64(DecodeError),
/// IO error with context.
2021-12-02 13:12:52 +00:00
Io {
2021-12-03 09:37:22 +00:00
/// The underlying IO error.
source: IoError,
/// Description of the error context.
2021-12-02 13:12:52 +00:00
context: String,
},
2021-12-03 09:37:22 +00:00
/// Scuttlebutt secret handshake error.
Handshake(HandshakeError),
/// Kuska SSB API error.
Api(ApiError),
/// Kuska SSB feed error.
Feed(FeedError),
/// Kuska SSB RPC error.
Rpc(RpcError),
/// Go-sbot error.
2021-12-02 13:12:52 +00:00
Sbot(String),
/// SSB sigil-link error.
SigilLink(String),
2021-12-03 09:37:22 +00:00
/// JSON serialization or deserialization error.
SerdeJson(JsonError),
/// Error decoding typed SSB message from content.
2021-12-30 18:35:39 +00:00
ContentType(String),
2021-12-30 01:08:55 +00:00
/// Error decoding UTF8 string from bytes
Utf8Parse {
/// The underlying parse error.
source: Utf8Error,
},
2021-12-02 13:12:52 +00:00
}
impl std::error::Error for GolgiError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
GolgiError::DecodeBase64(ref err) => Some(err),
GolgiError::Io { ref source, .. } => Some(source),
GolgiError::Handshake(_) => None,
2021-12-03 09:37:22 +00:00
GolgiError::Api(ref err) => Some(err),
GolgiError::Feed(ref err) => Some(err),
GolgiError::Rpc(ref err) => Some(err),
2021-12-02 13:12:52 +00:00
GolgiError::Sbot(_) => None,
GolgiError::SigilLink(_) => None,
2021-12-02 13:12:52 +00:00
GolgiError::SerdeJson(ref err) => Some(err),
2021-12-30 18:35:39 +00:00
GolgiError::ContentType(_) => None,
2022-01-05 18:58:48 +00:00
GolgiError::Utf8Parse { ref source } => Some(source),
2021-12-02 13:12:52 +00:00
}
}
}
impl std::fmt::Display for GolgiError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
// TODO: add context (what were we trying to decode?)
GolgiError::DecodeBase64(_) => write!(f, "Failed to decode base64"),
GolgiError::Io { ref context, .. } => write!(f, "IO error: {}", context),
2022-02-08 12:17:30 +00:00
GolgiError::Handshake(ref err) => write!(f, "Handshake failure: {}", err),
2021-12-03 09:37:22 +00:00
GolgiError::Api(ref err) => write!(f, "SSB API failure: {}", err),
GolgiError::Feed(ref err) => write!(f, "SSB feed error: {}", err),
2021-12-02 13:12:52 +00:00
// TODO: improve this variant with a context message
// then have the core display msg be: "SSB RPC error: {}", context
2021-12-03 09:37:22 +00:00
GolgiError::Rpc(ref err) => write!(f, "SSB RPC failure: {}", err),
2022-07-15 09:16:46 +00:00
GolgiError::Sbot(ref err) => write!(f, "Sbot encountered an error: {}", err),
2022-02-25 06:52:26 +00:00
GolgiError::SigilLink(ref context) => write!(f, "SSB blob ID error: {}", context),
2021-12-02 13:12:52 +00:00
GolgiError::SerdeJson(_) => write!(f, "Failed to serialize JSON slice"),
2021-12-03 09:37:22 +00:00
//GolgiError::WhoAmI(ref err) => write!(f, "{}", err),
2021-12-30 18:35:39 +00:00
GolgiError::ContentType(ref err) => write!(
f,
"Failed to decode typed message from SSB message content: {}",
err
),
2022-01-05 18:58:48 +00:00
GolgiError::Utf8Parse { source } => {
write!(f, "Failed to deserialize UTF8 from bytes: {}", source)
}
2021-12-02 13:12:52 +00:00
}
}
}
2021-12-03 09:37:22 +00:00
impl From<DecodeError> for GolgiError {
fn from(err: DecodeError) -> Self {
2021-12-02 13:12:52 +00:00
GolgiError::DecodeBase64(err)
}
}
2021-12-03 09:37:22 +00:00
impl From<HandshakeError> for GolgiError {
fn from(err: HandshakeError) -> Self {
2021-12-02 13:12:52 +00:00
GolgiError::Handshake(err)
}
}
2021-12-03 09:37:22 +00:00
impl From<ApiError> for GolgiError {
fn from(err: ApiError) -> Self {
GolgiError::Api(err)
2021-12-02 13:12:52 +00:00
}
}
2021-12-03 09:37:22 +00:00
impl From<FeedError> for GolgiError {
fn from(err: FeedError) -> Self {
GolgiError::Feed(err)
2021-12-02 13:12:52 +00:00
}
}
2021-12-03 09:37:22 +00:00
impl From<RpcError> for GolgiError {
fn from(err: RpcError) -> Self {
GolgiError::Rpc(err)
2021-12-02 13:12:52 +00:00
}
}
2021-12-03 09:37:22 +00:00
impl From<JsonError> for GolgiError {
fn from(err: JsonError) -> Self {
2021-12-02 13:12:52 +00:00
GolgiError::SerdeJson(err)
}
}
2021-12-30 01:08:55 +00:00
impl From<Utf8Error> for GolgiError {
fn from(err: Utf8Error) -> Self {
2022-01-05 18:58:48 +00:00
GolgiError::Utf8Parse { source: err }
2021-12-30 01:08:55 +00:00
}
}