2021-12-03 09:37:22 +00:00
|
|
|
//! Custom error type for `golgi`.
|
2021-12-02 13:12:52 +00:00
|
|
|
|
2021-12-03 09:37:22 +00:00
|
|
|
use std::io::Error as IoError;
|
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),
|
2021-12-03 09:37:22 +00:00
|
|
|
/// JSON serialization or deserialization error.
|
|
|
|
SerdeJson(JsonError),
|
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::SerdeJson(ref err) => Some(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
GolgiError::Handshake(ref err) => write!(f, "{}", 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),
|
2021-12-02 13:12:52 +00:00
|
|
|
GolgiError::Sbot(ref err) => write!(f, "Sbot returned an error response: {}", err),
|
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
}
|