Browse Source

Improve the error messages a bit

Ian Goldberg 5 months ago
parent
commit
1dd53d20c3
2 changed files with 54 additions and 13 deletions
  1. 46 7
      cmzcred_derive/src/lib.rs
  2. 8 6
      src/lib.rs

+ 46 - 7
cmzcred_derive/src/lib.rs

@@ -233,6 +233,16 @@ impl Parse for ShowSpec {
     }
 }
 
+impl ShowSpec {
+    fn abbr(&self) -> &'static str {
+        match self {
+            Self::Hide => "H",
+            Self::Reveal => "R",
+            Self::Implicit => "I",
+        }
+    }
+}
+
 // The possible attribute specifications for a credential to be issued
 #[derive(Copy, Clone, Debug, PartialEq)]
 enum IssueSpec {
@@ -257,6 +267,18 @@ impl Parse for IssueSpec {
     }
 }
 
+impl IssueSpec {
+    fn abbr(&self) -> &'static str {
+        match self {
+            Self::Hide => "H",
+            Self::Reveal => "R",
+            Self::Implicit => "I",
+            Self::Set => "S",
+            Self::Joint => "J",
+        }
+    }
+}
+
 // An attribute specification like "attr1: Reveal"
 #[derive(Clone)]
 struct AttrSpec<ShowOrIssue: Parse> {
@@ -505,9 +527,13 @@ fn protocol_macro(
         let mut cred_hide_joint = false;
         let iss_cred_id = format_ident!("cred_{}", iss_cred.id);
         for (attr, &spec) in iss_cred.attrs.iter() {
-            // The scoped attribute name, which will be the credential
-            // name, underscore, attribute name.
-            let scoped_attr = format_ident!("attr_{}_{}", iss_cred.id, attr);
+            // String versions of the credential name and the attribute
+            // name
+            let cred_str = iss_cred.id.to_string();
+            let attr_str = attr.to_string();
+
+            // The scoped attribute name
+            let scoped_attr = format_ident!("iss_{}attr_{}_{}", spec.abbr(), iss_cred.id, attr);
 
             if spec == IssueSpec::Hide || spec == IssueSpec::Joint {
                 cred_hide_joint = true;
@@ -556,7 +582,9 @@ fn protocol_macro(
                 request_fields.push_scalar(&scoped_attr.to_string());
                 prepare_code = quote! {
                     #prepare_code
-                    let #scoped_attr = #iss_cred_id.#attr.ok_or(CMZError::RevealAttrMissing)?;
+                    let #scoped_attr =
+                    #iss_cred_id.#attr.ok_or(CMZError::RevealAttrMissing(#cred_str,
+                    #attr_str))?;
                 };
                 handle_code_pre_fill = quote! {
                     #handle_code_pre_fill
@@ -571,11 +599,15 @@ fn protocol_macro(
             if spec == IssueSpec::Implicit {
                 prepare_code = quote! {
                     #prepare_code
-                    let #scoped_attr = #iss_cred_id.#attr.ok_or(CMZError::ImplicitAttrMissing)?;
+                    let #scoped_attr =
+                    #iss_cred_id.#attr.ok_or(CMZError::ImplicitAttrCliMissing(#cred_str,
+                    #attr_str))?;
                 };
                 handle_code_post_fill = quote! {
                     #handle_code_post_fill
-                    let #scoped_attr = #iss_cred_id.#attr.ok_or(CMZError::ImplicitAttrMissing)?;
+                    let #scoped_attr =
+                    #iss_cred_id.#attr.ok_or(CMZError::ImplicitAttrIssMissing(#cred_str,
+                    #attr_str))?;
                 }
             }
 
@@ -587,8 +619,15 @@ fn protocol_macro(
                 reply_fields.push_scalar(&scoped_attr.to_string());
                 handle_code_post_auth = quote! {
                     #handle_code_post_auth
-                    let #scoped_attr = #iss_cred_id.#attr.ok_or(CMZError::SetAttrMissing)?;
+                    let #scoped_attr =
+                    #iss_cred_id.#attr.ok_or(CMZError::SetAttrMissing(#cred_str,
+                    #attr_str))?;
                 };
+                finalize_code = quote! {
+                    #finalize_code
+                    let #scoped_attr = reply.#scoped_attr;
+                    #iss_cred_id.#attr = Some(#scoped_attr);
+                }
             }
             if spec == IssueSpec::Joint {
                 reply_fields.push_scalar(&scoped_attr.to_string());

+ 8 - 6
src/lib.rs

@@ -382,12 +382,14 @@ macro_rules! CMZ {
 /// functions generated by the CMZProtocol family of macros
 #[derive(Error, Debug)]
 pub enum CMZError {
-    #[error("Reveal attribute was not passed to prepare")]
-    RevealAttrMissing,
-    #[error("Implicit attribute was not passed to prepare or handle")]
-    ImplicitAttrMissing,
-    #[error("Set attribute was not passed to handle")]
-    SetAttrMissing,
+    #[error("Reveal attribute {1} of credential {0} was not passed to prepare")]
+    RevealAttrMissing(&'static str, &'static str),
+    #[error("Implicit attribute {1} of credential {0} was not passed to prepare")]
+    ImplicitAttrCliMissing(&'static str, &'static str),
+    #[error("Implicit attribute {1} of credential {0} was not set by fill_creds")]
+    ImplicitAttrIssMissing(&'static str, &'static str),
+    #[error("Set attribute {1} of credential {0} was not set by fill_creds")]
+    SetAttrMissing(&'static str, &'static str),
     #[error("unknown CMZ proof error")]
     Unknown,
 }