Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding error message field to AttributeError #120

Open
Veetaha opened this issue Jul 15, 2020 · 2 comments
Open

Consider adding error message field to AttributeError #120

Veetaha opened this issue Jul 15, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@Veetaha
Copy link
Contributor

Veetaha commented Jul 15, 2020

💡 Feature description

Consider adding a message field to AttributeError.
It is useful for debug purposes to know what exactly might have failed when parsing the attributes instead of
information-less AttributeError::InvalidType and AttributeError::InvalidFormat.
It might look like this:

pub enum AttributeError {
-    InvalidFormat,
+    InvalidFormat { message: String },
-    InvalidType,
+    InvalidType { message: String },
    MissingField {
        name: String,
    },
}

💻 Basic example

In my case I try to follow single-table dynamodb design.

So I created a db::Id struct which represents an string of format kind#uuid:
image

The code is this:

struct Id<K>(Uuid, PhantomData<*mut K>);

// ToString and FromStr implementation for Id<K> is skipped...

impl<K: HasKindTag> dynomite::Attribute for Id<K> {
    fn into_attr(self: Self) -> dynamodb::AttributeValue {
        self.to_string().into_attr()
    }
    fn from_attr(value: dynamodb::AttributeValue) -> Result<Self, dynomite::AttributeError> {
        value
            .s
             // we lose the info about the original type in the error message here
            .ok_or_else(|| dynomite::AttributeError::InvalidType)?
            .parse()
            // Unfortunatelly we lose error message from parsing here too...
            .map_err(|_err| dynomite::AttributeError::InvalidFormat)
    }
}
@softprops
Copy link
Owner

This sounds doable. Thanks for the design feedback

@softprops
Copy link
Owner

For context on the current types, both encode information in their type but have room for improvement in messaging details

dynomite::AttributeError::InvalidType

  • provides information to users that the type of a value is invalid given the context
  • an improvement could be what type is given

dynomite::AttributeError::InvalidFormat

  • for data types that require coercion, this provides information that the underlying data is in an invalid format
  • improvement here could be to surface the values contents

Since information is provided let's not call call this "informationless". Let's call the improvement "information rich"

@softprops softprops added the enhancement New feature or request label Jul 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants