mirror of
https://github.com/fluencelabs/trust-graph-test
synced 2025-07-30 21:22:16 +00:00
structurize errors
This commit is contained in:
1
wasm/Cargo.lock
generated
1
wasm/Cargo.lock
generated
@@ -441,6 +441,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"serde_with",
|
||||
"signature",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@@ -1,4 +1,5 @@
|
||||
use crate::dto::Certificate;
|
||||
use crate::service_api::ServiceError;
|
||||
use fluence::fce;
|
||||
|
||||
#[fce]
|
||||
@@ -7,8 +8,8 @@ pub struct InsertResult {
|
||||
pub error: String,
|
||||
}
|
||||
|
||||
impl From<Result<(), String>> for InsertResult {
|
||||
fn from(result: Result<(), String>) -> Self {
|
||||
impl From<Result<(), ServiceError>> for InsertResult {
|
||||
fn from(result: Result<(), ServiceError>) -> Self {
|
||||
match result {
|
||||
Ok(()) => InsertResult {
|
||||
ret_code: 0,
|
||||
@@ -16,7 +17,7 @@ impl From<Result<(), String>> for InsertResult {
|
||||
},
|
||||
Err(e) => InsertResult {
|
||||
ret_code: 1,
|
||||
error: e,
|
||||
error: format!("{}", e),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -29,8 +30,8 @@ pub struct WeightResult {
|
||||
pub error: String,
|
||||
}
|
||||
|
||||
impl From<Result<Option<u32>, String>> for WeightResult {
|
||||
fn from(result: Result<Option<u32>, String>) -> Self {
|
||||
impl From<Result<Option<u32>, ServiceError>> for WeightResult {
|
||||
fn from(result: Result<Option<u32>, ServiceError>) -> Self {
|
||||
match result {
|
||||
Ok(wo) => WeightResult {
|
||||
ret_code: 0,
|
||||
@@ -40,7 +41,7 @@ impl From<Result<Option<u32>, String>> for WeightResult {
|
||||
Err(e) => WeightResult {
|
||||
ret_code: 1,
|
||||
weight: vec![],
|
||||
error: e,
|
||||
error: format!("{}", e),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -53,8 +54,8 @@ pub struct AllCertsResult {
|
||||
pub error: String,
|
||||
}
|
||||
|
||||
impl From<Result<Vec<Certificate>, String>> for AllCertsResult {
|
||||
fn from(result: Result<Vec<Certificate>, String>) -> Self {
|
||||
impl From<Result<Vec<Certificate>, ServiceError>> for AllCertsResult {
|
||||
fn from(result: Result<Vec<Certificate>, ServiceError>) -> Self {
|
||||
match result {
|
||||
Ok(certs) => AllCertsResult {
|
||||
ret_code: 0,
|
||||
@@ -64,7 +65,7 @@ impl From<Result<Vec<Certificate>, String>> for AllCertsResult {
|
||||
Err(e) => AllCertsResult {
|
||||
ret_code: 1,
|
||||
certificates: vec![],
|
||||
error: e,
|
||||
error: format!("{}", e),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@@ -1,19 +1,33 @@
|
||||
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;
|
||||
use fluence_identity::{KeyPair, PublicKey};
|
||||
use std::convert::Into;
|
||||
use std::str::FromStr;
|
||||
use std::time::Duration;
|
||||
use thiserror::Error as ThisError;
|
||||
use trust_graph::{CertificateError, TrustGraphError};
|
||||
|
||||
fn insert_cert_impl(certificate: String, duration: u64) -> Result<(), String> {
|
||||
#[derive(ThisError, Debug)]
|
||||
pub enum ServiceError {
|
||||
#[error("{0}")]
|
||||
PublicKeyDecodeError(PKError),
|
||||
|
||||
#[error("{0}")]
|
||||
TGError(TrustGraphError),
|
||||
#[error("{0}")]
|
||||
CertError(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(|e| format!("{}", e))?;
|
||||
let certificate = trust_graph::Certificate::from_str(&certificate).map_err(CertError)?;
|
||||
|
||||
let mut tg = get_data().lock();
|
||||
tg.add(certificate, duration)?;
|
||||
tg.add(certificate, duration).map_err(TGError)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -23,12 +37,12 @@ fn insert_cert(certificate: String, duration: u64) -> InsertResult {
|
||||
insert_cert_impl(certificate, duration).into()
|
||||
}
|
||||
|
||||
fn get_weight_impl(public_key: String) -> Result<Option<u32>, String> {
|
||||
fn get_weight_impl(public_key: String) -> Result<Option<u32>, ServiceError> {
|
||||
let tg = get_data().lock();
|
||||
|
||||
let public_key = string_to_public_key(public_key)?;
|
||||
|
||||
let weight = tg.weight(public_key)?;
|
||||
let weight = tg.weight(public_key).map_err(TGError)?;
|
||||
|
||||
Ok(weight)
|
||||
}
|
||||
@@ -38,12 +52,8 @@ fn get_weight(public_key: String) -> WeightResult {
|
||||
get_weight_impl(public_key).into()
|
||||
}
|
||||
|
||||
fn string_to_public_key(public_key: String) -> Result<PublicKey, String> {
|
||||
let public_key = bs58::decode(public_key)
|
||||
.into_vec()
|
||||
.map_err(|e| format!("Couldn't decode public_key from base58: {}", e))?;
|
||||
let public_key = PublicKey::from_bytes(&public_key)
|
||||
.map_err(|e| format!("Couldn't decode public_key: {}", e))?;
|
||||
fn string_to_public_key(public_key: String) -> Result<PublicKey, ServiceError> {
|
||||
let public_key = PublicKey::from_base58(&public_key).map_err(PublicKeyDecodeError)?;
|
||||
|
||||
Ok(public_key)
|
||||
}
|
||||
@@ -53,11 +63,11 @@ fn get_all_certs(issued_for: String) -> AllCertsResult {
|
||||
get_all_certs_impl(issued_for).into()
|
||||
}
|
||||
|
||||
fn get_all_certs_impl(issued_for: String) -> Result<Vec<Certificate>, String> {
|
||||
fn get_all_certs_impl(issued_for: String) -> Result<Vec<Certificate>, ServiceError> {
|
||||
let tg = get_data().lock();
|
||||
|
||||
let public_key = string_to_public_key(issued_for)?;
|
||||
let certs = tg.get_all_certs(public_key, &[])?;
|
||||
let certs = tg.get_all_certs(public_key, &[]).map_err(TGError)?;
|
||||
Ok(certs.into_iter().map(|c| c.into()).collect())
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user