diff --git a/src/trust.rs b/src/trust.rs index 5336cd7..4567676 100644 --- a/src/trust.rs +++ b/src/trust.rs @@ -15,7 +15,8 @@ */ use crate::trust::TrustError::{ - DecodeError, IncorrectTrustLength, PublicKeyError, SignatureError, SignatureFromBytesError, + Base58DecodeError, DecodePublicKeyError, IncorrectTrustLength, ParseError, PublicKeyError, + SignatureError, SignatureFromBytesError, }; use derivative::Derivative; use fluence_identity::key_pair::KeyPair; @@ -23,6 +24,7 @@ use fluence_identity::public_key::{PKError, PublicKey}; use fluence_identity::signature::Signature; use serde::{Deserialize, Serialize}; use std::convert::TryInto; +use std::num::ParseIntError; use std::time::Duration; use thiserror::Error as ThisError; @@ -70,8 +72,14 @@ pub enum TrustError { SignatureError(ed25519_dalek::SignatureError), /// Errors occured on trust decoding from differrent formats - #[error("{0}")] - DecodeError(String), + #[error("Cannot decode the public key: {0} in the trust: {1}")] + 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}")] SignatureFromBytesError(signature::Error), @@ -193,21 +201,15 @@ impl Trust { } fn bs58_str_to_vec(str: &str, field: &str) -> Result, TrustError> { - bs58::decode(str).into_vec().map_err(|e| { - DecodeError(format!( - "Cannot decode `{}` from base58 format in the trust '{}': {}", - field, str, e - )) - }) + bs58::decode(str) + .into_vec() + .map_err(|e| Base58DecodeError(field.to_string(), str.to_string(), e)) } fn str_to_duration(str: &str, field: &str) -> Result { - let secs = str.parse().map_err(|e| { - DecodeError(format!( - "Cannot parse `{}` field in the trust '{}': {}", - field, str, e - )) - })?; + let secs = str + .parse() + .map_err(|e| ParseError(field.to_string(), str.to_string(), e))?; Ok(Duration::from_secs(secs)) } @@ -219,12 +221,8 @@ impl Trust { ) -> Result { // PublicKey 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| { - DecodeError(format!( - "Cannot decode the public key: {} in the trust '{}'", - issued_for, e - )) - })?; + let issued_for = PublicKey::from_bytes(issued_for_bytes.as_slice()) + .map_err(|e| DecodePublicKeyError(issued_for.to_string(), e))?; // 64 bytes signature let signature = Self::bs58_str_to_vec(signature, "signature")?; diff --git a/wasm/src/service_api.rs b/wasm/src/service_api.rs index e81a925..a05f775 100644 --- a/wasm/src/service_api.rs +++ b/wasm/src/service_api.rs @@ -1,6 +1,5 @@ use crate::dto::Certificate; use crate::results::{AllCertsResult, InsertResult, WeightResult}; -use crate::service_api::ServiceError::{CertError, PublicKeyDecodeError, TGError}; use crate::storage_impl::get_data; use fluence::fce; use fluence_identity::public_key::PKError; @@ -14,20 +13,20 @@ use trust_graph::{CertificateError, TrustGraphError}; #[derive(ThisError, Debug)] pub enum ServiceError { #[error("{0}")] - PublicKeyDecodeError(PKError), + PublicKeyDecodeError(#[from] PKError), #[error("{0}")] - TGError(TrustGraphError), + TGError(#[from] TrustGraphError), #[error("{0}")] - CertError(CertificateError), + CertError(#[from] CertificateError), } fn insert_cert_impl(certificate: String, duration: u64) -> Result<(), ServiceError> { 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(); - tg.add(certificate, duration).map_err(TGError)?; + tg.add(certificate, duration)?; Ok(()) } @@ -42,7 +41,7 @@ fn get_weight_impl(public_key: String) -> Result, ServiceError> { 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) } @@ -53,7 +52,7 @@ fn get_weight(public_key: String) -> WeightResult { } fn string_to_public_key(public_key: String) -> Result { - let public_key = PublicKey::from_base58(&public_key).map_err(PublicKeyDecodeError)?; + let public_key = PublicKey::from_base58(&public_key)?; Ok(public_key) } @@ -67,7 +66,7 @@ fn get_all_certs_impl(issued_for: String) -> Result, ServiceErr let tg = get_data().lock(); 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()) }