Skip to content

Commit

Permalink
Refactor error handling (#702)
Browse files Browse the repository at this point in the history
Refactor error handling across the board, pull logic down from api-c layer

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas authored Dec 24, 2022
1 parent 70ec685 commit dedb0e4
Show file tree
Hide file tree
Showing 215 changed files with 4,589 additions and 5,193 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 17 additions & 46 deletions agency_client/src/agency_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::sync::Arc;
use url::Url;

use crate::configuration::AgencyClientConfig;
use crate::error::AgencyClientResult;
use crate::errors::error::{AgencyClientError, AgencyClientErrorKind, AgencyClientResult};
use crate::wallet::base_agency_client_wallet::{BaseAgencyClientWallet, StubAgencyClientWallet};
use crate::{validation, AgencyClientError, AgencyClientErrorKind};
use crate::utils::validation;

#[derive(Clone, Debug)]
pub struct AgencyClient {
Expand All @@ -18,20 +18,7 @@ pub struct AgencyClient {
pub my_vk: String,
}

pub fn validate_mandotory_config_val<F, S, E>(
val: &str,
err: AgencyClientErrorKind,
closure: F,
) -> AgencyClientResult<()>
where
F: Fn(&str) -> Result<S, E>,
{
closure(val).or(Err(AgencyClientError::from(err)))?;
Ok(())
}

impl AgencyClient {

pub fn get_wallet(&self) -> Arc<dyn BaseAgencyClientWallet> {
Arc::clone(&self.wallet)
}
Expand Down Expand Up @@ -90,37 +77,21 @@ impl AgencyClient {
pub fn configure(mut self, wallet: Arc<dyn BaseAgencyClientWallet>, config: &AgencyClientConfig) -> AgencyClientResult<Self> {
info!("AgencyClient::configure >>> config {:?}", config);

validate_mandotory_config_val(
&config.agency_did,
AgencyClientErrorKind::InvalidDid,
validation::validate_did,
)?;
validate_mandotory_config_val(
&config.agency_verkey,
AgencyClientErrorKind::InvalidVerkey,
validation::validate_verkey,
)?;
validate_mandotory_config_val(
&config.sdk_to_remote_did,
AgencyClientErrorKind::InvalidDid,
validation::validate_did,
)?;
validate_mandotory_config_val(
&config.sdk_to_remote_verkey,
AgencyClientErrorKind::InvalidVerkey,
validation::validate_verkey,
)?;
validate_mandotory_config_val(
&config.remote_to_sdk_did,
AgencyClientErrorKind::InvalidDid,
validation::validate_did,
)?;
validate_mandotory_config_val(
&config.remote_to_sdk_verkey,
AgencyClientErrorKind::InvalidVerkey,
validation::validate_verkey,
)?;
validate_mandotory_config_val(&config.agency_endpoint, AgencyClientErrorKind::InvalidUrl, Url::parse)?;
validation::validate_did(&config.agency_did)?;
validation::validate_verkey(&config.agency_verkey)?;
validation::validate_did(&config.sdk_to_remote_did)?;
validation::validate_verkey(&config.sdk_to_remote_verkey)?;
validation::validate_did(&config.remote_to_sdk_did)?;
validation::validate_verkey(&config.remote_to_sdk_verkey)?;

match Url::parse(&config.agency_endpoint) {
Err(_) => Err(AgencyClientError::from_msg(
AgencyClientErrorKind::InvalidUrl,
format!("Endpoint {} is not valid url", &config.agency_endpoint),
)),
_ => Ok(())
}?;


self.set_agency_url(&config.agency_endpoint);
self.set_agency_did(&config.agency_did);
Expand Down
7 changes: 5 additions & 2 deletions agency_client/src/api/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::messages::update_com_method::{ComMethodType, UpdateComMethod};
use crate::messages::update_connection::DeleteConnectionBuilder;
use crate::testing::mocking::{agency_mocks_enabled, AgencyMock};
use crate::testing::{mocking, test_constants};
use crate::{AgencyClientError, AgencyClientErrorKind, AgencyClientResult};
use crate::errors::error::{AgencyClientError, AgencyClientErrorKind, AgencyClientResult};

impl AgencyClient {
pub async fn delete_connection_agent(
Expand Down Expand Up @@ -61,7 +61,10 @@ impl AgencyClient {

match response.remove(0) {
Client2AgencyMessage::CreateKeyResponse(res) => Ok((res.for_did, res.for_verkey)),
_ => Err(AgencyClientError::from(AgencyClientErrorKind::InvalidHttpResponse)),
res @ _ => Err(AgencyClientError::from_msg(
AgencyClientErrorKind::InvalidHttpResponse,
format!("Expected to response of Client2AgencyMessage::CreateKeyResponse, but received: {:?}", res),
)),
}
}

Expand Down
2 changes: 1 addition & 1 deletion agency_client/src/api/downloaded_message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::error::{AgencyClientError, AgencyClientErrorKind, AgencyClientResult};
use crate::errors::error::{AgencyClientError, AgencyClientErrorKind, AgencyClientResult};
use crate::utils::encryption_envelope::EncryptionEnvelope;
use crate::MessageStatusCode;
use crate::wallet::base_agency_client_wallet::BaseAgencyClientWallet;
Expand Down
3 changes: 2 additions & 1 deletion agency_client/src/api/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::messages::get_messages::GetMessagesBuilder;
use crate::messages::update_message::{UIDsByConn, UpdateMessageStatusByConnectionsBuilder};
use crate::testing::mocking::AgencyMock;
use crate::testing::{mocking, test_constants};
use crate::{AgencyClientError, AgencyClientErrorKind, AgencyClientResult, MessageStatusCode};
use crate::MessageStatusCode;
use crate::errors::error::{AgencyClientError, AgencyClientErrorKind, AgencyClientResult};

impl AgencyClient {
pub async fn update_messages(
Expand Down
2 changes: 1 addition & 1 deletion agency_client/src/api/onboarding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use crate::agency_client::AgencyClient;
use crate::configuration::AgencyClientConfig;
use crate::error::{AgencyClientError, AgencyClientErrorKind, AgencyClientResult};
use crate::errors::error::{AgencyClientError, AgencyClientErrorKind, AgencyClientResult};
use crate::messages::a2a_message::Client2AgencyMessage;
use crate::messages::connect::{Connect, ConnectResponse};
use crate::messages::create_agent::{CreateAgent, CreateAgentResponse};
Expand Down
161 changes: 0 additions & 161 deletions agency_client/src/error.rs

This file was deleted.

Loading

0 comments on commit dedb0e4

Please sign in to comment.