Use thiserror

This commit is contained in:
adria0 2020-06-20 21:22:19 +02:00
parent 96573e4023
commit 81467b27d1
10 changed files with 93 additions and 202 deletions

View File

@ -26,6 +26,7 @@ get_if_addrs = "0.5.3"
regex = "1.3.7"
once_cell = "1.3.1"
async-stream = "0.2.1"
thiserror = "1.0.20"
[[example]]
name = "ssb-cli"

Binary file not shown.

View File

@ -91,7 +91,7 @@ where
RecvMsg::ErrorResponse(message) => {
return std::result::Result::Err(Box::new(AppError::new(message)));
}
_ => { }
_ => {}
}
}
}
@ -119,7 +119,7 @@ where
return std::result::Result::Err(Box::new(AppError::new(message)));
}
RecvMsg::CancelStreamRespose() => break,
_ => { }
_ => {}
}
}
}
@ -189,34 +189,37 @@ async fn main() -> SolarResult<()> {
let req_id = client.whoami_req_send().await?;
let whoami = match get_async(&mut rpc_reader, req_id, whoami_res_parse).await {
Ok(res) => {
println!("😊 server says hello to {}", res.id);
id
}
Err(err) => {
if !err.to_string().contains("method:whoami is not in list of allowed methods") {
println!("Cannot ask for whoami {}",err);
}
id
}
Ok(res) => {
println!("😊 server says hello to {}", res.id);
id
}
Err(err) => {
if !err
.to_string()
.contains("method:whoami is not in list of allowed methods")
{
println!("Cannot ask for whoami {}", err);
}
id
}
};
loop {
let mut line_buffer = String::new();
print!("> "); std::io::stdout().flush();
let mut line_buffer = String::new();
print!("> ");
let _ = std::io::stdout().flush();
match std::io::stdin().read_line(&mut line_buffer) {
Err(_) => break,
_ => { }
Err(_) => break,
_ => {}
};
let args: Vec<String> = line_buffer
.replace("\n", "")
.split_whitespace()
.map(|arg| arg.to_string())
.collect();
if args.len() == 0 {
continue;
continue;
}
match (args[0].as_str(), args.len()) {
("exit", 1) => {

View File

@ -1,59 +1,11 @@
#[derive(Debug)]
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
InvalidSequenceNo,
ServerMessage(String),
Rpc(crate::rpc::Error),
InvalidInviteCode,
HomeNotFound,
InvalidConfig,
Json(serde_json::Error),
ParseInt(std::num::ParseIntError),
CryptoFormat(crate::crypto::Error),
Io(std::io::Error),
Feed(crate::feed::Error),
#[error("rpc")]
Rpc(#[from] crate::rpc::Error),
#[error("json decode")]
Json(#[from] serde_json::Error),
}
impl From<crate::rpc::Error> for Error {
fn from(err: crate::rpc::Error) -> Self {
Error::Rpc(err)
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error::Json(err)
}
}
impl From<std::num::ParseIntError> for Error {
fn from(err: std::num::ParseIntError) -> Self {
Error::ParseInt(err)
}
}
impl From<crate::crypto::Error> for Error {
fn from(err: crate::crypto::Error) -> Self {
Error::CryptoFormat(err)
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Io(err)
}
}
impl From<crate::feed::Error> for Error {
fn from(err: crate::feed::Error) -> Self {
Error::Feed(err)
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;

View File

@ -1,23 +1,19 @@
#[derive(Debug)]
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
Base64Decode(base64::DecodeError),
#[error("error decoding base64")]
Base64Decode(#[from] base64::DecodeError),
#[error("bad public key")]
BadPublicKey,
#[error("bad secret key")]
BadSecretKey,
#[error("invalid digest")]
InvalidDigest,
#[error("invalid suffix")]
InvalidSuffix,
#[error("cannot create signature")]
CannotCreateSignature,
}
impl From<base64::DecodeError> for Error {
fn from(err: base64::DecodeError) -> Self {
Error::Base64Decode(err)
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;

View File

@ -1,35 +1,17 @@
#[derive(Debug)]
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
ParseInt(std::num::ParseIntError),
#[error("invalid integer")]
ParseInt(#[from] std::num::ParseIntError),
#[error("invalid invite code")]
InvalidInviteCode,
#[error("invalid broadcast message")]
InvalidBroadcastMessage,
CryptoFormat(crate::crypto::Error),
Io(std::io::Error),
#[error("invalid crypto format")]
CryptoFormat(#[from] crate::crypto::Error),
#[error("i/o")]
Io(#[from] std::io::Error),
}
impl From<crate::crypto::Error> for Error {
fn from(err: crate::crypto::Error) -> Self {
Error::CryptoFormat(err)
}
}
impl From<std::num::ParseIntError> for Error {
fn from(err: std::num::ParseIntError) -> Self {
Error::ParseInt(err)
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Io(err)
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;

View File

@ -1,49 +1,35 @@
#[derive(Debug)]
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
Base64Decode(base64::DecodeError),
#[error("base64 decoding")]
Base64Decode(#[from] base64::DecodeError),
#[error("feed digest mismatch")]
FeedDigestMismatch,
SystemTimeError(std::time::SystemTimeError),
#[error("time error")]
SystemTimeError(#[from] std::time::SystemTimeError),
#[error("invalid json")]
InvalidJson,
#[error("invalid signature")]
InvalidSignature,
#[error("failed to decipher")]
FailedToDecipher,
#[error("cannot create key")]
CannotCreateKey,
#[error("cannot read nonce")]
CannotReadNonce,
#[error("crypto scalar mult failed")]
CryptoScalarMultFailed,
#[error("empty plaintext")]
EmptyPlaintext,
#[error("bad recipent")]
BadRecipientCount,
#[error("invalid key from group")]
CryptoKeyFromGrupFailed,
CryptoFormat(crate::crypto::Error),
Json(serde_json::Error),
#[error("invalid key format")]
CryptoFormat(#[from] crate::crypto::Error),
#[error("invalid json")]
Json(#[from] serde_json::Error),
}
impl From<base64::DecodeError> for Error {
fn from(err: base64::DecodeError) -> Self {
Error::Base64Decode(err)
}
}
impl From<crate::crypto::Error> for Error {
fn from(err: crate::crypto::Error) -> Self {
Error::CryptoFormat(err)
}
}
impl From<std::time::SystemTimeError> for Error {
fn from(err: std::time::SystemTimeError) -> Self {
Error::SystemTimeError(err)
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error::Json(err)
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;

View File

@ -1,34 +1,17 @@
#[derive(Debug)]
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("$HOME not found")]
HomeNotFound,
#[error("invalid configuration file")]
InvalidConfig,
Serde(serde_json::Error),
CryptoFormat(crate::crypto::Error),
SyncIo(std::io::Error),
#[error("json deserialization")]
Serde(#[from] serde_json::Error),
#[error("crypto format")]
CryptoFormat(#[from] crate::crypto::Error),
#[error("i/o")]
SyncIo(#[from] std::io::Error),
}
impl From<crate::crypto::Error> for Error {
fn from(err: crate::crypto::Error) -> Self {
Error::CryptoFormat(err)
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::SyncIo(err)
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error::Serde(err)
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;

View File

@ -4,6 +4,7 @@ pub extern crate kuska_handshake as handshake;
extern crate serde;
extern crate async_std;
extern crate serde_json;
extern crate thiserror;
pub mod api;
pub mod crypto;

View File

@ -1,28 +1,15 @@
#[derive(Debug)]
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("header size too small")]
HeaderSizeTooSmall,
#[error("invalid body type: {0}")]
InvalidBodyType(u8),
Io(async_std::io::Error),
Json(serde_json::Error),
#[error("i/o")]
Io(#[from] async_std::io::Error),
#[error("json decoding")]
Json(#[from] serde_json::Error),
}
impl From<async_std::io::Error> for Error {
fn from(err: async_std::io::Error) -> Self {
Error::Io(err)
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error::Json(err)
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;