107 lines
3.4 KiB
Rust
107 lines
3.4 KiB
Rust
/*
|
|
use async_std::{io::Read, net::TcpStream};
|
|
use std::fmt::Debug;
|
|
|
|
use kuska_handshake::async_std::BoxStream;
|
|
use kuska_sodiumoxide::crypto::sign::ed25519;
|
|
use kuska_ssb::api::{dto::WhoAmIOut, ApiCaller};
|
|
use kuska_ssb::discovery;
|
|
use kuska_ssb::keystore;
|
|
use kuska_ssb::keystore::OwnedIdentity;
|
|
use kuska_ssb::rpc::{RecvMsg, RequestNo, RpcReader, RpcWriter};
|
|
|
|
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
|
|
|
pub fn whoami_res_parse(body: &[u8]) -> Result<WhoAmIOut> {
|
|
Ok(serde_json::from_slice(body)?)
|
|
}
|
|
*/
|
|
|
|
#[derive(Debug)]
|
|
pub enum GolgiError {
|
|
DecodeBase64(base64::DecodeError),
|
|
Io {
|
|
source: std::io::Error,
|
|
context: String,
|
|
},
|
|
Handshake(kuska_handshake::async_std::Error),
|
|
KuskaApi(kuska_ssb::api::Error),
|
|
KuskaFeed(kuska_ssb::feed::Error),
|
|
KuskaRpc(kuska_ssb::rpc::Error),
|
|
// error message returned from the go-sbot
|
|
Sbot(String),
|
|
SerdeJson(serde_json::Error),
|
|
WhoAmI(String),
|
|
}
|
|
|
|
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,
|
|
GolgiError::KuskaApi(ref err) => Some(err),
|
|
GolgiError::KuskaFeed(ref err) => Some(err),
|
|
GolgiError::KuskaRpc(ref err) => Some(err),
|
|
GolgiError::Sbot(_) => None,
|
|
GolgiError::SerdeJson(ref err) => Some(err),
|
|
GolgiError::WhoAmI(_) => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
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),
|
|
GolgiError::KuskaApi(_) => write!(f, "SSB API failure"),
|
|
GolgiError::KuskaFeed(_) => write!(f, "SSB feed error"),
|
|
// TODO: improve this variant with a context message
|
|
// then have the core display msg be: "SSB RPC error: {}", context
|
|
GolgiError::KuskaRpc(_) => write!(f, "SSB RPC failure"),
|
|
GolgiError::Sbot(ref err) => write!(f, "Sbot returned an error response: {}", err),
|
|
GolgiError::SerdeJson(_) => write!(f, "Failed to serialize JSON slice"),
|
|
GolgiError::WhoAmI(ref err) => write!(f, "{}", err),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<base64::DecodeError> for GolgiError {
|
|
fn from(err: base64::DecodeError) -> Self {
|
|
GolgiError::DecodeBase64(err)
|
|
}
|
|
}
|
|
|
|
impl From<kuska_handshake::async_std::Error> for GolgiError {
|
|
fn from(err: kuska_handshake::async_std::Error) -> Self {
|
|
GolgiError::Handshake(err)
|
|
}
|
|
}
|
|
|
|
impl From<kuska_ssb::api::Error> for GolgiError {
|
|
fn from(err: kuska_ssb::api::Error) -> Self {
|
|
GolgiError::KuskaApi(err)
|
|
}
|
|
}
|
|
|
|
impl From<kuska_ssb::feed::Error> for GolgiError {
|
|
fn from(err: kuska_ssb::feed::Error) -> Self {
|
|
GolgiError::KuskaFeed(err)
|
|
}
|
|
}
|
|
|
|
impl From<kuska_ssb::rpc::Error> for GolgiError {
|
|
fn from(err: kuska_ssb::rpc::Error) -> Self {
|
|
GolgiError::KuskaRpc(err)
|
|
}
|
|
}
|
|
|
|
impl From<serde_json::Error> for GolgiError {
|
|
fn from(err: serde_json::Error) -> Self {
|
|
GolgiError::SerdeJson(err)
|
|
}
|
|
}
|