Skip to content

Commit

Permalink
Refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreenbury committed Sep 6, 2024
1 parent ac352c8 commit 62f9db9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
12 changes: 9 additions & 3 deletions trustchain-api/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,18 @@ pub trait TrustchainDataAPI {
let expected_hash = credential
.credential_subject
.to_single()
.unwrap() // TODO: handle error with ?
.ok_or(DataCredentialError::ManyCredentialSubject(
credential.credential_subject.clone(),
))?
.property_set
.as_ref()
.unwrap() // TODO: handle error with ?
.ok_or(DataCredentialError::MissingAttribute(
"property_set".to_string(),
))?
.get(DATA_ATTRIBUTE)
.unwrap() // TODO: handle error with ?
.ok_or(DataCredentialError::MissingAttribute(
DATA_ATTRIBUTE.to_string(),
))?
.as_str()
.expect("dataset attribute is a str");

Expand Down
6 changes: 6 additions & 0 deletions trustchain-cli/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Err(DataCredentialError::MismatchedHashDigests(_, _)) => {
println!("Digest... ❌ (mismatched data hash digests)");
}
Err(DataCredentialError::MissingAttribute(att)) => {
println!("Invalid credential... ❌ (missing attribute: \"{att}\")");
}
Err(DataCredentialError::ManyCredentialSubject(subjects)) => {
println!("Invalid credential... ❌ (only one subject permitted, multiple subjects found: {subjects:?})");
}
Ok(_) => {
println!("Proof.... ✅");
println!("Issuer... ✅");
Expand Down
8 changes: 7 additions & 1 deletion trustchain-core/src/vc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Verifiable credential functionality for Trustchain.
use crate::verifier::VerifierError;
use ssi::vc::VerificationResult;
use ssi::vc::{CredentialSubject, OneOrMany, VerificationResult};
use thiserror::Error;

/// An error relating to verifiable credentials and presentations.
Expand Down Expand Up @@ -35,6 +35,12 @@ pub enum DataCredentialError {
/// Hash digests do not match.
#[error("Hash digests do not match. Expected: {0}. Actual: {1}.")]
MismatchedHashDigests(String, String),
/// Multiple credential subjects
#[error("Multiple credential subjects: {0:?}")]
ManyCredentialSubject(OneOrMany<CredentialSubject>),
/// Missing attribute
#[error("Missing attribute: {0}")]
MissingAttribute(String),
}

impl From<CredentialError> for DataCredentialError {
Expand Down

0 comments on commit 62f9db9

Please sign in to comment.