Skip to content

Commit

Permalink
Add backtrace capturing, prototype a test err handling
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <[email protected]>
  • Loading branch information
Patrik-Stas committed Oct 22, 2023
1 parent 1a414f0 commit 56dbb4b
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 151 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions aries_vcx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ default = ["credx", "vdrtools_wallet"]
credx = ["aries_vcx_core/credx"]
vdr_proxy_ledger = ["aries_vcx_core/vdr_proxy_ledger", "aries_vcx_core/vdrtools_wallet"]
vdrtools_wallet = ["aries_vcx_core/vdrtools_wallet"]
backtrace_errors = ["backtrace"]

# Feature for allowing legacy proof verification
legacy_proof = ["aries_vcx_core/legacy_proof"]
Expand Down Expand Up @@ -49,6 +50,8 @@ derive_builder = "0.12.0"
tokio = { version = "1.20.4" }
thiserror = "1.0.37"
url = { version = "2.3", features = ["serde"] }
backtrace = { optional = true, version = "0.3" }


[target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.13.3"
Expand Down
1 change: 1 addition & 0 deletions aries_vcx/src/common/anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[allow(clippy::unwrap_used)]
pub mod integration_tests {
use aries_vcx_core::{
anoncreds::base_anoncreds::BaseAnonCreds,
errors::error::AriesVcxCoreErrorKind,
ledger::{base_ledger::AnoncredsLedgerRead, indy::pool::test_utils::get_temp_dir_path},
};
Expand Down
91 changes: 72 additions & 19 deletions aries_vcx/src/errors/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,33 +165,84 @@ pub enum AriesVcxErrorKind {
InvalidMessageFormat,
}

#[derive(Debug, thiserror::Error)]
#[derive(thiserror::Error)]
pub struct AriesVcxError {
msg: String,
kind: AriesVcxErrorKind,
backtrace: Option<String>,
}

fn format_error(err: &AriesVcxError, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "Error: {}", err.msg())?;
match err.backtrace() {
None => {}
Some(backtrace) => {
writeln!(f, "Backtrace: {}", backtrace)?;
}
}
let mut current = err.source();
while let Some(cause) = current {
writeln!(f, "Caused by:\n{}", cause)?;
current = cause.source();
}
Ok(())
}

impl fmt::Display for AriesVcxError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "Error: {}\n", self.msg)?;
let mut current = self.source();
while let Some(cause) = current {
writeln!(f, "Caused by:\n\t{}", cause)?;
current = cause.source();
format_error(self, f)
}
}

impl fmt::Debug for AriesVcxError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
format_error(self, f)
}
}

fn try_capture_backtrace() -> Option<String> {
#[cfg(feature = "backtrace_errors")]
{
use backtrace::Backtrace;

let backtrace = Backtrace::new();
let mut filtered_backtrace = String::new();

for frame in backtrace.frames() {
let symbols = frame.symbols();
if !symbols.is_empty() {
let symbol = &symbols[0];
if let Some(filename) = symbol.filename() {
if let Some(line) = symbol.lineno() {
filtered_backtrace.push_str(&format!("[{}:{}]", filename.display(), line));
}
}
if let Some(name) = symbol.name() {
filtered_backtrace.push_str(&format!(" {}", name));
}
filtered_backtrace.push('\n');
}
}
Ok(())
return Some(filtered_backtrace);
}
#[cfg(not(feature = "backtrace_errors"))]
None
}

impl AriesVcxError {
fn new(kind: AriesVcxErrorKind, msg: String) -> Self {
AriesVcxError {
msg,
kind,
backtrace: try_capture_backtrace(),
}
}

pub fn from_msg<D>(kind: AriesVcxErrorKind, msg: D) -> AriesVcxError
where
D: fmt::Display + fmt::Debug + Send + Sync + 'static,
{
AriesVcxError {
msg: msg.to_string(),
kind,
}
Self::new(kind, msg.to_string())
}

pub fn find_root_cause(&self) -> String {
Expand All @@ -209,24 +260,26 @@ impl AriesVcxError {
self.kind
}

pub fn msg(&self) -> &str {
&self.msg
}

pub fn backtrace(&self) -> Option<&String> {
self.backtrace.as_ref()
}

pub fn extend<D>(self, msg: D) -> AriesVcxError
where
D: fmt::Display + fmt::Debug + Send + Sync + 'static,
{
AriesVcxError {
msg: msg.to_string(),
..self
}
Self::new(self.kind, format!("{}\n{}", self.msg, msg))
}

pub fn map<D>(self, kind: AriesVcxErrorKind, msg: D) -> AriesVcxError
where
D: fmt::Display + fmt::Debug + Send + Sync + 'static,
{
AriesVcxError {
msg: msg.to_string(),
kind,
}
Self::new(kind, msg.to_string())
}
}

Expand Down
1 change: 1 addition & 0 deletions aries_vcx/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(backtrace_frames)]
#![allow(clippy::large_enum_variant)]
#![allow(clippy::diverging_sub_expression)]
//this is needed for some large json macro invocations
Expand Down
83 changes: 43 additions & 40 deletions aries_vcx/src/utils/devsetup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use aries_vcx_core::{
request_submitter::vdr_ledger::{IndyVdrLedgerPool, IndyVdrSubmitter},
response_cacher::in_memory::{InMemoryResponseCacher, InMemoryResponseCacherConfig},
},
wallet::base_wallet::BaseWallet,
wallet::{base_wallet::BaseWallet, indy::IndySdkWallet},
PoolConfig, ResponseParser,
};
#[cfg(feature = "vdr_proxy_ledger")]
Expand All @@ -43,7 +43,11 @@ use super::ledger::{indyvdr_build_ledger_read, indyvdr_build_ledger_write};
use crate::{
errors::error::VcxResult,
global::settings,
utils::{constants::POOL1_TXN, file::write_file, test_logger::LibvcxDefaultLogger},
utils::{
constants::{POOL1_TXN, TRUSTEE_SEED},
file::write_file,
test_logger::LibvcxDefaultLogger,
},
};

const DEFAULT_AML_LABEL: &str = "eula";
Expand Down Expand Up @@ -267,47 +271,46 @@ pub async fn dev_build_featured_profile(
(MockLedger, MockLedger, MockAnoncreds)
}

pub async fn test_setup() -> SetupProfile<
impl IndyLedgerRead + AnoncredsLedgerRead + Sized,
impl IndyLedgerWrite + AnoncredsLedgerWrite + Sized,
impl BaseAnonCreds + Sized,
IndySdkWallet,
> {
use aries_vcx_core::anoncreds::base_anoncreds::BaseAnonCreds;

init_test_logging();

let genesis_file_path = get_temp_file_path(POOL1_TXN).to_str().unwrap().to_string();
create_testpool_genesis_txn_file(&genesis_file_path);

let (public_did, wallet_handle) = dev_setup_wallet_indy(TRUSTEE_SEED).await;
let wallet = IndySdkWallet::new(wallet_handle);
let (ledger_read, ledger_write, anoncreds) =
dev_build_featured_profile(genesis_file_path.clone()).await;
anoncreds
.prover_create_link_secret(
&wallet,
aries_vcx_core::global::settings::DEFAULT_LINK_SECRET_ALIAS,
)
.await
.unwrap();

SetupProfile::new(
ledger_read,
ledger_write,
anoncreds,
wallet,
public_did.to_string(),
genesis_file_path,
)
.await
}

#[macro_export]
macro_rules! run_setup {
($func:expr) => {{
use aries_vcx_core::anoncreds::base_anoncreds::BaseAnonCreds;

$crate::utils::devsetup::init_test_logging();

let genesis_file_path = aries_vcx_core::ledger::indy::pool::test_utils::get_temp_file_path(
$crate::utils::constants::POOL1_TXN,
)
.to_str()
.unwrap()
.to_string();
aries_vcx_core::ledger::indy::pool::test_utils::create_testpool_genesis_txn_file(
&genesis_file_path,
);

let (public_did, wallet_handle) =
$crate::utils::devsetup::dev_setup_wallet_indy($crate::utils::constants::TRUSTEE_SEED)
.await;
let wallet = aries_vcx_core::wallet::indy::IndySdkWallet::new(wallet_handle);
let (ledger_read, ledger_write, anoncreds) =
$crate::utils::devsetup::dev_build_featured_profile(genesis_file_path.clone()).await;
anoncreds
.prover_create_link_secret(
&wallet,
aries_vcx_core::global::settings::DEFAULT_LINK_SECRET_ALIAS,
)
.await
.unwrap();

$crate::utils::devsetup::SetupProfile::new(
ledger_read,
ledger_write,
anoncreds,
wallet,
public_did.to_string(),
genesis_file_path,
)
.await
.run($func)
$crate::utils::devsetup::test_setup().await.run($func)
}};
}

Expand Down
Loading

0 comments on commit 56dbb4b

Please sign in to comment.