Skip to content
Snippets Groups Projects
Commit b7f09da2 authored by Éloïs's avatar Éloïs
Browse files

[feat] gva:bca: impl Error for BcaReqExecError

parent b9e073a1
No related branches found
No related tags found
1 merge request!1364Bca
...@@ -1166,6 +1166,7 @@ dependencies = [ ...@@ -1166,6 +1166,7 @@ dependencies = [
"bincode", "bincode",
"dubp", "dubp",
"serde", "serde",
"thiserror",
] ]
[[package]] [[package]]
......
...@@ -34,7 +34,7 @@ use async_bincode::AsyncBincodeReader; ...@@ -34,7 +34,7 @@ use async_bincode::AsyncBincodeReader;
use bincode::Options as _; use bincode::Options as _;
use dubp::crypto::keys::{ed25519::Ed25519KeyPair, Signator}; use dubp::crypto::keys::{ed25519::Ed25519KeyPair, Signator};
use duniter_bca_types::{ use duniter_bca_types::{
bincode_opts, BcaReq, BcaReqTypeV0, BcaResp, BcaRespTypeV0, BcaRespV0, ReqExecError, bincode_opts, BcaReq, BcaReqExecError, BcaReqTypeV0, BcaResp, BcaRespTypeV0, BcaRespV0,
}; };
use duniter_dbs::{FileBackend, SharedDbs}; use duniter_dbs::{FileBackend, SharedDbs};
use futures::{prelude::stream::FuturesUnordered, StreamExt, TryStream, TryStreamExt}; use futures::{prelude::stream::FuturesUnordered, StreamExt, TryStream, TryStreamExt};
...@@ -146,11 +146,11 @@ impl BcaExecutor { ...@@ -146,11 +146,11 @@ impl BcaExecutor {
let resp = match req_res { let resp = match req_res {
Ok(resp) => Ok(resp), Ok(resp) => Ok(resp),
Err(e) => Err(if e.is_cancelled() { Err(e) => Err(if e.is_cancelled() {
ReqExecError::Cancelled BcaReqExecError::Cancelled
} else if e.is_panic() { } else if e.is_panic() {
ReqExecError::Panic BcaReqExecError::Panic
} else { } else {
ReqExecError::Unknown BcaReqExecError::Unknown
}), }),
}; };
let mut resp_buffer = RespBytes::new(); let mut resp_buffer = RespBytes::new();
...@@ -163,8 +163,8 @@ impl BcaExecutor { ...@@ -163,8 +163,8 @@ impl BcaExecutor {
.await .await
} }
Err(e) => { Err(e) => {
let req_res: Result<BcaResp, ReqExecError> = let req_res: Result<BcaResp, BcaReqExecError> =
Err(ReqExecError::InvalidReq(e.to_string())); Err(BcaReqExecError::InvalidReq(e.to_string()));
let mut resp_buffer = RespBytes::new(); let mut resp_buffer = RespBytes::new();
bincode_opts() bincode_opts()
.serialize_into(&mut resp_buffer, &req_res) .serialize_into(&mut resp_buffer, &req_res)
...@@ -294,8 +294,8 @@ mod tests { ...@@ -294,8 +294,8 @@ mod tests {
//println!("bytes={:?}", bytes); //println!("bytes={:?}", bytes);
let bytes_res = bca_executor.execute::<&[u8]>(&bytes[..], false).await; let bytes_res = bca_executor.execute::<&[u8]>(&bytes[..], false).await;
//println!("bytes_res={:?}", bytes_res); //println!("bytes_res={:?}", bytes_res);
let bca_res: Vec<Result<BcaResp, ReqExecError>> = let bca_res: Vec<Result<BcaResp, BcaReqExecError>> =
AsyncBincodeReader::<_, Result<BcaResp, ReqExecError>>::from(&bytes_res[..]) AsyncBincodeReader::<_, Result<BcaResp, BcaReqExecError>>::from(&bytes_res[..])
.try_collect::<Vec<_>>() .try_collect::<Vec<_>>()
.await?; .await?;
...@@ -332,14 +332,14 @@ mod tests { ...@@ -332,14 +332,14 @@ mod tests {
//println!("bytes={:?}", bytes); //println!("bytes={:?}", bytes);
let bytes_res = bca_executor.execute::<&[u8]>(&bytes[..], false).await; let bytes_res = bca_executor.execute::<&[u8]>(&bytes[..], false).await;
//println!("bytes_res={:?}", bytes_res); //println!("bytes_res={:?}", bytes_res);
let bca_res: Vec<Result<BcaResp, ReqExecError>> = let bca_res: Vec<Result<BcaResp, BcaReqExecError>> =
AsyncBincodeReader::<_, Result<BcaResp, ReqExecError>>::from(&bytes_res[..]) AsyncBincodeReader::<_, Result<BcaResp, BcaReqExecError>>::from(&bytes_res[..])
.try_collect::<Vec<_>>() .try_collect::<Vec<_>>()
.await?; .await?;
assert_eq!( assert_eq!(
bca_res, bca_res,
vec![Err(ReqExecError::InvalidReq( vec![Err(BcaReqExecError::InvalidReq(
"io error: unexpected end of file".to_owned() "io error: unexpected end of file".to_owned()
))] ))]
); );
...@@ -376,8 +376,8 @@ mod tests { ...@@ -376,8 +376,8 @@ mod tests {
//println!("bytes={:?}", bytes); //println!("bytes={:?}", bytes);
let bytes_res = bca_executor.execute::<&[u8]>(&bytes[..], false).await; let bytes_res = bca_executor.execute::<&[u8]>(&bytes[..], false).await;
//println!("bytes_res={:?}", bytes_res); //println!("bytes_res={:?}", bytes_res);
let bca_res: Vec<Result<BcaResp, ReqExecError>> = let bca_res: Vec<Result<BcaResp, BcaReqExecError>> =
AsyncBincodeReader::<_, Result<BcaResp, ReqExecError>>::from(&bytes_res[..]) AsyncBincodeReader::<_, Result<BcaResp, BcaReqExecError>>::from(&bytes_res[..])
.try_collect::<Vec<_>>() .try_collect::<Vec<_>>()
.await?; .await?;
......
...@@ -9,6 +9,7 @@ edition = "2018" ...@@ -9,6 +9,7 @@ edition = "2018"
bincode = "1.3" bincode = "1.3"
dubp = { version = "0.49.0" } dubp = { version = "0.49.0" }
serde = { version = "1.0.105", features = ["derive"] } serde = { version = "1.0.105", features = ["derive"] }
thiserror = "1.0.20"
[features] [features]
default = ["duniter"] default = ["duniter"]
......
...@@ -31,6 +31,7 @@ use dubp::crypto::hashs::Hash; ...@@ -31,6 +31,7 @@ use dubp::crypto::hashs::Hash;
use dubp::crypto::keys::ed25519::{PublicKey, Signature}; use dubp::crypto::keys::ed25519::{PublicKey, Signature};
use dubp::wallet::prelude::*; use dubp::wallet::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use thiserror::Error;
pub fn bincode_opts() -> impl bincode::Options { pub fn bincode_opts() -> impl bincode::Options {
bincode::options() bincode::options()
...@@ -83,12 +84,16 @@ pub enum BcaRespTypeV0 { ...@@ -83,12 +84,16 @@ pub enum BcaRespTypeV0 {
Pong, Pong,
} }
pub type BcaResult = Vec<Result<BcaResp, ReqExecError>>; pub type BcaResult = Vec<Result<BcaResp, BcaReqExecError>>;
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)] #[derive(Clone, Debug, Deserialize, Error, PartialEq, Eq, Serialize)]
pub enum ReqExecError { pub enum BcaReqExecError {
#[error("task cancelled")]
Cancelled, Cancelled,
#[error("Invalid request: {0}")]
InvalidReq(String), InvalidReq(String),
#[error("task panicked")]
Panic, Panic,
#[error("Unknown error")]
Unknown, Unknown,
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment