Skip to content

Commit 7c2ab22

Browse files
committed
Remove repetition in handling credential error
1 parent b61dffb commit 7c2ab22

File tree

1 file changed

+30
-44
lines changed

1 file changed

+30
-44
lines changed

trustchain-cli/src/bin/main.rs

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use trustchain_api::{
1313
};
1414
use trustchain_cli::config::cli_config;
1515
use trustchain_core::{
16+
chain::DIDChain,
1617
vc::{CredentialError, DataCredentialError},
1718
verifier::Verifier,
1819
};
@@ -252,26 +253,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
252253
.await;
253254
// Handle result
254255
match verify_result {
255-
_err @ Err(CredentialError::VerificationResultError(_)) => {
256-
println!("Proof... ❌ Invalid");
257-
}
258-
_err @ Err(CredentialError::NoProofPresent) => {
259-
println!("Proof... ❌ (missing proof)");
260-
}
261-
_err @ Err(CredentialError::MissingVerificationMethod) => {
262-
println!("Proof... ❌ (missing verification method)");
263-
}
264-
_err @ Err(CredentialError::NoIssuerPresent) => {
265-
println!("Proof... ✅");
266-
println!("Issuer... ❌ (missing issuer)");
267-
}
268-
_err @ Err(CredentialError::VerifierError(_)) => {
269-
println!("Proof... ✅");
270-
println!("Issuer... ❌ (with verifier error)");
271-
}
272-
_err @ Err(CredentialError::FailedToDecodeJWT) => {
273-
println!("Proof... ❌");
274-
println!("Issuer... ❌");
256+
Err(cred_err) => {
257+
handle_credential_error(&cred_err);
275258
}
276259
Ok(_) => {
277260
println!("Proof... ✅");
@@ -363,32 +346,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
363346
)
364347
.await;
365348
// Handle result
366-
// TODO: avoid repetition (see similar error handling above).
367349
match verify_result {
368350
ref _e @ Err(DataCredentialError::CredentialError(ref cred_err)) => {
369-
match cred_err {
370-
_err @ CredentialError::VerificationResultError(_) => {
371-
println!("Proof... ❌ Invalid");
372-
}
373-
_err @ CredentialError::NoProofPresent => {
374-
println!("Proof... ❌ (missing proof)");
375-
}
376-
_err @ CredentialError::MissingVerificationMethod => {
377-
println!("Proof... ❌ (missing verification method)");
378-
}
379-
_err @ CredentialError::NoIssuerPresent => {
380-
println!("Proof... ✅");
381-
println!("Issuer... ❌ (missing issuer)");
382-
}
383-
_err @ CredentialError::VerifierError(_) => {
384-
println!("Proof... ✅");
385-
println!("Issuer... ❌ (with verifier error)");
386-
}
387-
_err @ CredentialError::FailedToDecodeJWT => {
388-
println!("Proof... ❌");
389-
println!("Issuer... ❌");
390-
}
391-
}
351+
handle_credential_error(cred_err);
392352
}
393353
_e @ Err(DataCredentialError::MismatchedHashDigests(_, _)) => {
394354
println!("Digest... ❌ (mismatched dataset hash digests)");
@@ -437,3 +397,29 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
437397
}
438398
Ok(())
439399
}
400+
401+
fn handle_credential_error(err: &CredentialError) {
402+
match err {
403+
_err @ CredentialError::VerificationResultError(_) => {
404+
println!("Proof... ❌ Invalid");
405+
}
406+
_err @ CredentialError::NoProofPresent => {
407+
println!("Proof... ❌ (missing proof)");
408+
}
409+
_err @ CredentialError::MissingVerificationMethod => {
410+
println!("Proof... ❌ (missing verification method)");
411+
}
412+
_err @ CredentialError::NoIssuerPresent => {
413+
println!("Proof... ✅");
414+
println!("Issuer... ❌ (missing issuer)");
415+
}
416+
_err @ CredentialError::VerifierError(_) => {
417+
println!("Proof... ✅");
418+
println!("Issuer... ❌ (with verifier error)");
419+
}
420+
_err @ CredentialError::FailedToDecodeJWT => {
421+
println!("Proof... ❌");
422+
println!("Issuer... ❌");
423+
}
424+
}
425+
}

0 commit comments

Comments
 (0)