From b33c40e24d57250e56033a6089f400252545a100 Mon Sep 17 00:00:00 2001 From: Patrik Stas Date: Tue, 5 Dec 2023 15:00:40 +0100 Subject: [PATCH] Err handling refactor Signed-off-by: Patrik Stas --- aries/aries_vcx/src/errors/error.rs | 2 + aries/aries_vcx/src/errors/mapping_others.rs | 97 ------------------- .../src/utils/encryption_envelope.rs | 15 +-- .../legacy/libvcx_core/src/errors/error.rs | 2 + .../src/errors/mapping_from_ariesvcx.rs | 1 + 5 files changed, 14 insertions(+), 103 deletions(-) diff --git a/aries/aries_vcx/src/errors/error.rs b/aries/aries_vcx/src/errors/error.rs index fb6dc35da4..3cc23a89b1 100644 --- a/aries/aries_vcx/src/errors/error.rs +++ b/aries/aries_vcx/src/errors/error.rs @@ -13,6 +13,8 @@ pub enum AriesVcxErrorKind { InvalidState, #[error("Invalid Configuration")] InvalidConfiguration, + #[error("Authentication error")] + AuthenticationError, #[error("Invalid JSON string")] InvalidJson, #[error("Invalid Option")] diff --git a/aries/aries_vcx/src/errors/mapping_others.rs b/aries/aries_vcx/src/errors/mapping_others.rs index 192ec1ced1..ec0a675d37 100644 --- a/aries/aries_vcx/src/errors/mapping_others.rs +++ b/aries/aries_vcx/src/errors/mapping_others.rs @@ -171,100 +171,3 @@ impl From for AriesVcxError { AriesVcxError::from_msg(kind, format!("AriesVcxCoreError: {}", err)) } } - -// TODO -impl From for AriesVcxCoreError { - fn from(err: AriesVcxError) -> Self { - let kind = match err.kind() { - AriesVcxErrorKind::InvalidState => AriesVcxCoreErrorKind::InvalidState, - AriesVcxErrorKind::InvalidConfiguration => AriesVcxCoreErrorKind::InvalidConfiguration, - AriesVcxErrorKind::InvalidJson => AriesVcxCoreErrorKind::InvalidJson, - AriesVcxErrorKind::InvalidOption => AriesVcxCoreErrorKind::InvalidOption, - AriesVcxErrorKind::InvalidMessagePack => AriesVcxCoreErrorKind::InvalidMessagePack, - AriesVcxErrorKind::NotReady => AriesVcxCoreErrorKind::NotReady, - AriesVcxErrorKind::IOError => AriesVcxCoreErrorKind::IOError, - AriesVcxErrorKind::LibindyInvalidStructure => { - AriesVcxCoreErrorKind::LibindyInvalidStructure - } - AriesVcxErrorKind::InvalidLibindyParam => AriesVcxCoreErrorKind::InvalidLibindyParam, - AriesVcxErrorKind::ActionNotSupported => AriesVcxCoreErrorKind::ActionNotSupported, - AriesVcxErrorKind::InvalidInput => AriesVcxCoreErrorKind::InvalidInput, - AriesVcxErrorKind::UnimplementedFeature => AriesVcxCoreErrorKind::UnimplementedFeature, - AriesVcxErrorKind::CredDefAlreadyCreated => { - AriesVcxCoreErrorKind::CredDefAlreadyCreated - } - AriesVcxErrorKind::RevDeltaNotFound => AriesVcxCoreErrorKind::RevDeltaNotFound, - AriesVcxErrorKind::RevDeltaFailedToClear => { - AriesVcxCoreErrorKind::RevDeltaFailedToClear - } - AriesVcxErrorKind::CreateRevRegDef => AriesVcxCoreErrorKind::CreateRevRegDef, - AriesVcxErrorKind::InvalidRevocationDetails => { - AriesVcxCoreErrorKind::InvalidRevocationDetails - } - AriesVcxErrorKind::InvalidRevocationEntry => { - AriesVcxCoreErrorKind::InvalidRevocationEntry - } - AriesVcxErrorKind::InvalidRevocationTimestamp => { - AriesVcxCoreErrorKind::InvalidRevocationTimestamp - } - AriesVcxErrorKind::RevRegDefNotFound => AriesVcxCoreErrorKind::RevRegDefNotFound, - AriesVcxErrorKind::InvalidAttributesStructure => { - AriesVcxCoreErrorKind::InvalidAttributesStructure - } - AriesVcxErrorKind::InvalidProof => AriesVcxCoreErrorKind::InvalidProof, - AriesVcxErrorKind::InvalidSchema => AriesVcxCoreErrorKind::InvalidSchema, - AriesVcxErrorKind::InvalidProofCredentialData => { - AriesVcxCoreErrorKind::InvalidProofCredentialData - } - AriesVcxErrorKind::InvalidProofRequest => AriesVcxCoreErrorKind::InvalidProofRequest, - AriesVcxErrorKind::ProofRejected => AriesVcxCoreErrorKind::ProofRejected, - AriesVcxErrorKind::InvalidSchemaSeqNo => AriesVcxCoreErrorKind::InvalidSchemaSeqNo, - AriesVcxErrorKind::DuplicationSchema => AriesVcxCoreErrorKind::DuplicationSchema, - AriesVcxErrorKind::UnknownSchemaRejection => { - AriesVcxCoreErrorKind::UnknownSchemaRejection - } - AriesVcxErrorKind::InvalidGenesisTxnPath => { - AriesVcxCoreErrorKind::InvalidGenesisTxnPath - } - AriesVcxErrorKind::CreatePoolConfig => AriesVcxCoreErrorKind::CreatePoolConfig, - AriesVcxErrorKind::PoolLedgerConnect => AriesVcxCoreErrorKind::PoolLedgerConnect, - AriesVcxErrorKind::InvalidLedgerResponse => { - AriesVcxCoreErrorKind::InvalidLedgerResponse - } - AriesVcxErrorKind::LedgerItemNotFound => AriesVcxCoreErrorKind::LedgerItemNotFound, - AriesVcxErrorKind::NoPoolOpen => AriesVcxCoreErrorKind::NoPoolOpen, - AriesVcxErrorKind::PostMessageFailed => AriesVcxCoreErrorKind::PostMessageFailed, - AriesVcxErrorKind::WalletCreate => AriesVcxCoreErrorKind::WalletCreate, - AriesVcxErrorKind::WalletAccessFailed => AriesVcxCoreErrorKind::WalletAccessFailed, - AriesVcxErrorKind::InvalidWalletHandle => AriesVcxCoreErrorKind::InvalidWalletHandle, - AriesVcxErrorKind::DuplicationWallet => AriesVcxCoreErrorKind::DuplicationWallet, - AriesVcxErrorKind::WalletRecordNotFound => AriesVcxCoreErrorKind::WalletRecordNotFound, - AriesVcxErrorKind::DuplicationWalletRecord => { - AriesVcxCoreErrorKind::DuplicationWalletRecord - } - AriesVcxErrorKind::WalletNotFound => AriesVcxCoreErrorKind::WalletNotFound, - AriesVcxErrorKind::WalletAlreadyOpen => AriesVcxCoreErrorKind::WalletAlreadyOpen, - AriesVcxErrorKind::DuplicationMasterSecret => { - AriesVcxCoreErrorKind::DuplicationMasterSecret - } - AriesVcxErrorKind::DuplicationDid => AriesVcxCoreErrorKind::DuplicationDid, - AriesVcxErrorKind::LoggingError => AriesVcxCoreErrorKind::LoggingError, - AriesVcxErrorKind::EncodeError => AriesVcxCoreErrorKind::EncodeError, - AriesVcxErrorKind::UnknownError => AriesVcxCoreErrorKind::UnknownError, - AriesVcxErrorKind::InvalidDid => AriesVcxCoreErrorKind::InvalidDid, - AriesVcxErrorKind::InvalidVerkey => AriesVcxCoreErrorKind::InvalidVerkey, - AriesVcxErrorKind::InvalidNonce => AriesVcxCoreErrorKind::InvalidNonce, - AriesVcxErrorKind::InvalidUrl => AriesVcxCoreErrorKind::InvalidUrl, - AriesVcxErrorKind::SerializationError => AriesVcxCoreErrorKind::SerializationError, - AriesVcxErrorKind::NotBase58 => AriesVcxCoreErrorKind::NotBase58, - AriesVcxErrorKind::ParsingError => AriesVcxCoreErrorKind::ParsingError, - AriesVcxErrorKind::InvalidHttpResponse => AriesVcxCoreErrorKind::InvalidHttpResponse, - AriesVcxErrorKind::InvalidMessages => AriesVcxCoreErrorKind::InvalidMessages, - AriesVcxErrorKind::VdrToolsError(u32) => AriesVcxCoreErrorKind::VdrToolsError(u32), - AriesVcxErrorKind::UrsaError => AriesVcxCoreErrorKind::UrsaError, - AriesVcxErrorKind::NoAgentInformation => AriesVcxCoreErrorKind::NoAgentInformation, - AriesVcxErrorKind::InvalidMessageFormat => AriesVcxCoreErrorKind::InvalidMessageFormat, - }; - AriesVcxCoreError::from_msg(kind, format!("AriesVcxError: {}", err)) - } -} diff --git a/aries/aries_vcx/src/utils/encryption_envelope.rs b/aries/aries_vcx/src/utils/encryption_envelope.rs index e210044fbd..8984f1912a 100644 --- a/aries/aries_vcx/src/utils/encryption_envelope.rs +++ b/aries/aries_vcx/src/utils/encryption_envelope.rs @@ -217,7 +217,7 @@ impl EncryptionEnvelope { sender_vk, expected_vk ); return Err(AriesVcxError::from_msg( - AriesVcxErrorKind::InvalidJson, + AriesVcxErrorKind::AuthenticationError, format!( "Message did not pass authentication check. Expected sender \ verkey was {}, but actually was {}", @@ -229,7 +229,7 @@ impl EncryptionEnvelope { None => { error!("auth_unpack message was authcrypted"); return Err(AriesVcxError::from_msg( - AriesVcxErrorKind::InvalidJson, + AriesVcxErrorKind::AuthenticationError, "Can't authenticate message because it was anoncrypted.", )); } @@ -388,9 +388,12 @@ pub mod unit_tests { .await .unwrap(); - let data_unpacked = - EncryptionEnvelope::auth_unpack(&setup.wallet, envelope.0, &sender_key_alice) - .await - .unwrap_err(); + let err = + EncryptionEnvelope::auth_unpack(&setup.wallet, envelope.0, &sender_key_alice).await; + assert!(err.is_err()); + assert_eq!( + err.unwrap_err().kind(), + AriesVcxErrorKind::AuthenticationError + ); } } diff --git a/aries/misc/legacy/libvcx_core/src/errors/error.rs b/aries/misc/legacy/libvcx_core/src/errors/error.rs index 4f0241e211..2a8b5c58fc 100644 --- a/aries/misc/legacy/libvcx_core/src/errors/error.rs +++ b/aries/misc/legacy/libvcx_core/src/errors/error.rs @@ -96,6 +96,8 @@ pub enum LibvcxErrorKind { InvalidProofRequest, #[error("The proof was rejected")] ProofRejected, + #[error("Authentication error")] + AuthenticationError, // Schema #[error("Could not create schema")] diff --git a/aries/misc/legacy/libvcx_core/src/errors/mapping_from_ariesvcx.rs b/aries/misc/legacy/libvcx_core/src/errors/mapping_from_ariesvcx.rs index 8cfe0ce84c..48ef005599 100644 --- a/aries/misc/legacy/libvcx_core/src/errors/mapping_from_ariesvcx.rs +++ b/aries/misc/legacy/libvcx_core/src/errors/mapping_from_ariesvcx.rs @@ -91,6 +91,7 @@ impl From for LibvcxErrorKind { AriesVcxErrorKind::LedgerItemNotFound => LibvcxErrorKind::LedgerItemNotFound, AriesVcxErrorKind::UrsaError => LibvcxErrorKind::UrsaError, AriesVcxErrorKind::ProofRejected => LibvcxErrorKind::ProofRejected, + AriesVcxErrorKind::AuthenticationError => LibvcxErrorKind::AuthenticationError, } } }