-
Notifications
You must be signed in to change notification settings - Fork 24
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
Support custom errors #161
Comments
I would love to see this - is one of the main reasons I'm unable to use nutype |
@AlisCode @gobanos @boyswan What do you guys think of the following syntax? #[nutype(
sanitize(trim),
validate(with = validate_name, error = NameError),
derive(Debug, AsRef, PartialEq, Deref),
)]
struct Name(String); where fn validate_name(name: &str) -> Result<(), NameError> {
if name.len() < 3 {
Err(NameError::TooShort)
} else if name.len() > 20 {
Err(NameError::TooLong)
} else {
Ok(())
}
}
// This actually probably will need to implement fully std::error::Error
#[derive(Debug)]
enum NameError {
TooShort,
TooLong,
} Do you have any alternative proposal to consider? |
Looks great! |
Looks cool! Although I have to say this breaks a lot with the current Right now,
I think it's a good feat of nutype that you're able to provide a multi-step validation, and this proposal attemps to do exactly the reverse which is that you would provide one custom validation function which would contain all your logic in one place. It would probably mean having a lot of code dedicated to the special case. What if instead, For example : struct TooLongError;
fn max_len(input: &str) -> Result<(), TooLongError> {
if name.len() > 20 {
Err(TooLongError)
} else {
Ok(())
}
}
struct TooShortError;
fn min_len(input: &str) -> Result<(), TooShortError> {
if name.len() < 3 {
Err(TooShortError)
} else {
Ok(())
}
}
#[nutype(
sanitize(trim),
validate(
with(max_len, variant = TooLong),
with(min_len, variant = TooShort),
)
derive(Debug, AsRef, PartialEq, Deref),
)]
struct Name(String); Produced code : enum NameError {
TooLong(TooLongError),
TooShort(TooShortError),
// And here you can support other potential validation steps ...
} Also right now |
@AlisCode Thank you for your valuable input! |
This would "pollute" (even more than it currently does) the public API of libraries that want to use |
Maybe good inspiration on how to handle "let users customize errors" can be taken from parser combinator libraries? Feels like Maybe there is something to copy from the work that @epage has done in winnow? Again, I don't know, just throwing random ideas around. |
@DJDuque Could you explain how |
@boyswan @AlisCode @DJDuque I'd appreciate if you find a bit of time to try it out and give me feedback before I release See the docs in the Custom validation with a custom error type section. |
I don't recall being involved in this issue, maybe you meant to tag someone else, @greyblake? |
@memark Sorry my bad. |
Context
Some orgs and people are not quite happy with auto-generated error types, e.g. it's not possible to customize them, inject extra details, etc.
Requirement
Provide a way for users to:
Result<(), CustomError>
, whereCustomError
is a user defined error.Technically it's possible, but the syntax for this needs to be decided.
Consideration
Having this may clash with "unified error": #75, so requires careful thinking.
The text was updated successfully, but these errors were encountered: