mirror of
https://github.com/fluencelabs/trust-graph-test
synced 2025-04-25 12:02:27 +00:00
structurize errors
This commit is contained in:
parent
1e9b8ba33b
commit
fb13bc1cdf
40
src/trust.rs
40
src/trust.rs
@ -15,7 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
use crate::trust::TrustError::{
|
use crate::trust::TrustError::{
|
||||||
DecodeError, IncorrectTrustLength, PublicKeyError, SignatureError, SignatureFromBytesError,
|
Base58DecodeError, DecodePublicKeyError, IncorrectTrustLength, ParseError, PublicKeyError,
|
||||||
|
SignatureError, SignatureFromBytesError,
|
||||||
};
|
};
|
||||||
use derivative::Derivative;
|
use derivative::Derivative;
|
||||||
use fluence_identity::key_pair::KeyPair;
|
use fluence_identity::key_pair::KeyPair;
|
||||||
@ -23,6 +24,7 @@ use fluence_identity::public_key::{PKError, PublicKey};
|
|||||||
use fluence_identity::signature::Signature;
|
use fluence_identity::signature::Signature;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
use std::num::ParseIntError;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use thiserror::Error as ThisError;
|
use thiserror::Error as ThisError;
|
||||||
|
|
||||||
@ -70,8 +72,14 @@ pub enum TrustError {
|
|||||||
SignatureError(ed25519_dalek::SignatureError),
|
SignatureError(ed25519_dalek::SignatureError),
|
||||||
|
|
||||||
/// Errors occured on trust decoding from differrent formats
|
/// Errors occured on trust decoding from differrent formats
|
||||||
#[error("{0}")]
|
#[error("Cannot decode the public key: {0} in the trust: {1}")]
|
||||||
DecodeError(String),
|
DecodePublicKeyError(String, PKError),
|
||||||
|
|
||||||
|
#[error("Cannot parse `{0}` field in the trust '{1}': {2}")]
|
||||||
|
ParseError(String, String, ParseIntError),
|
||||||
|
|
||||||
|
#[error("Cannot decode `{0}` from base58 format in the trust '{1}': {2}")]
|
||||||
|
Base58DecodeError(String, String, bs58::decode::Error),
|
||||||
|
|
||||||
#[error("Cannot decode a signature from bytes: {0}")]
|
#[error("Cannot decode a signature from bytes: {0}")]
|
||||||
SignatureFromBytesError(signature::Error),
|
SignatureFromBytesError(signature::Error),
|
||||||
@ -193,21 +201,15 @@ impl Trust {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bs58_str_to_vec(str: &str, field: &str) -> Result<Vec<u8>, TrustError> {
|
fn bs58_str_to_vec(str: &str, field: &str) -> Result<Vec<u8>, TrustError> {
|
||||||
bs58::decode(str).into_vec().map_err(|e| {
|
bs58::decode(str)
|
||||||
DecodeError(format!(
|
.into_vec()
|
||||||
"Cannot decode `{}` from base58 format in the trust '{}': {}",
|
.map_err(|e| Base58DecodeError(field.to_string(), str.to_string(), e))
|
||||||
field, str, e
|
|
||||||
))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn str_to_duration(str: &str, field: &str) -> Result<Duration, TrustError> {
|
fn str_to_duration(str: &str, field: &str) -> Result<Duration, TrustError> {
|
||||||
let secs = str.parse().map_err(|e| {
|
let secs = str
|
||||||
DecodeError(format!(
|
.parse()
|
||||||
"Cannot parse `{}` field in the trust '{}': {}",
|
.map_err(|e| ParseError(field.to_string(), str.to_string(), e))?;
|
||||||
field, str, e
|
|
||||||
))
|
|
||||||
})?;
|
|
||||||
Ok(Duration::from_secs(secs))
|
Ok(Duration::from_secs(secs))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,12 +221,8 @@ impl Trust {
|
|||||||
) -> Result<Self, TrustError> {
|
) -> Result<Self, TrustError> {
|
||||||
// PublicKey
|
// PublicKey
|
||||||
let issued_for_bytes = Self::bs58_str_to_vec(issued_for, "issued_for")?;
|
let issued_for_bytes = Self::bs58_str_to_vec(issued_for, "issued_for")?;
|
||||||
let issued_for = PublicKey::from_bytes(issued_for_bytes.as_slice()).map_err(|e| {
|
let issued_for = PublicKey::from_bytes(issued_for_bytes.as_slice())
|
||||||
DecodeError(format!(
|
.map_err(|e| DecodePublicKeyError(issued_for.to_string(), e))?;
|
||||||
"Cannot decode the public key: {} in the trust '{}'",
|
|
||||||
issued_for, e
|
|
||||||
))
|
|
||||||
})?;
|
|
||||||
|
|
||||||
// 64 bytes signature
|
// 64 bytes signature
|
||||||
let signature = Self::bs58_str_to_vec(signature, "signature")?;
|
let signature = Self::bs58_str_to_vec(signature, "signature")?;
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use crate::dto::Certificate;
|
use crate::dto::Certificate;
|
||||||
use crate::results::{AllCertsResult, InsertResult, WeightResult};
|
use crate::results::{AllCertsResult, InsertResult, WeightResult};
|
||||||
use crate::service_api::ServiceError::{CertError, PublicKeyDecodeError, TGError};
|
|
||||||
use crate::storage_impl::get_data;
|
use crate::storage_impl::get_data;
|
||||||
use fluence::fce;
|
use fluence::fce;
|
||||||
use fluence_identity::public_key::PKError;
|
use fluence_identity::public_key::PKError;
|
||||||
@ -14,20 +13,20 @@ use trust_graph::{CertificateError, TrustGraphError};
|
|||||||
#[derive(ThisError, Debug)]
|
#[derive(ThisError, Debug)]
|
||||||
pub enum ServiceError {
|
pub enum ServiceError {
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
PublicKeyDecodeError(PKError),
|
PublicKeyDecodeError(#[from] PKError),
|
||||||
|
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
TGError(TrustGraphError),
|
TGError(#[from] TrustGraphError),
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
CertError(CertificateError),
|
CertError(#[from] CertificateError),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_cert_impl(certificate: String, duration: u64) -> Result<(), ServiceError> {
|
fn insert_cert_impl(certificate: String, duration: u64) -> Result<(), ServiceError> {
|
||||||
let duration = Duration::from_millis(duration);
|
let duration = Duration::from_millis(duration);
|
||||||
let certificate = trust_graph::Certificate::from_str(&certificate).map_err(CertError)?;
|
let certificate = trust_graph::Certificate::from_str(&certificate)?;
|
||||||
|
|
||||||
let mut tg = get_data().lock();
|
let mut tg = get_data().lock();
|
||||||
tg.add(certificate, duration).map_err(TGError)?;
|
tg.add(certificate, duration)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,7 +41,7 @@ fn get_weight_impl(public_key: String) -> Result<Option<u32>, ServiceError> {
|
|||||||
|
|
||||||
let public_key = string_to_public_key(public_key)?;
|
let public_key = string_to_public_key(public_key)?;
|
||||||
|
|
||||||
let weight = tg.weight(public_key).map_err(TGError)?;
|
let weight = tg.weight(public_key)?;
|
||||||
|
|
||||||
Ok(weight)
|
Ok(weight)
|
||||||
}
|
}
|
||||||
@ -53,7 +52,7 @@ fn get_weight(public_key: String) -> WeightResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn string_to_public_key(public_key: String) -> Result<PublicKey, ServiceError> {
|
fn string_to_public_key(public_key: String) -> Result<PublicKey, ServiceError> {
|
||||||
let public_key = PublicKey::from_base58(&public_key).map_err(PublicKeyDecodeError)?;
|
let public_key = PublicKey::from_base58(&public_key)?;
|
||||||
|
|
||||||
Ok(public_key)
|
Ok(public_key)
|
||||||
}
|
}
|
||||||
@ -67,7 +66,7 @@ fn get_all_certs_impl(issued_for: String) -> Result<Vec<Certificate>, ServiceErr
|
|||||||
let tg = get_data().lock();
|
let tg = get_data().lock();
|
||||||
|
|
||||||
let public_key = string_to_public_key(issued_for)?;
|
let public_key = string_to_public_key(issued_for)?;
|
||||||
let certs = tg.get_all_certs(public_key, &[]).map_err(TGError)?;
|
let certs = tg.get_all_certs(public_key, &[])?;
|
||||||
Ok(certs.into_iter().map(|c| c.into()).collect())
|
Ok(certs.into_iter().map(|c| c.into()).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user