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
Custom error example #194
Custom error example #194
Conversation
If the build() method cannot convert the validation error to the generated builder error, this is a problem with the validation function. The error span should point there, not at the macro call-site. Due to some issue in `darling` or `syn`, this change is pointing at the closing quotation instead of the whole path; that is being tracked as TedDriggs/darling#120.
Structured errors need a safe way to act on uninitialized fields that doesn't conflate with validation errors. This commit adds that by providing a struct to represent an uninitialized field and generating conversions from that into the auto-generated error type.
@TedDriggs I am afraid of the situation when user will provide different non-compatible parameters and will receive a cryptic error message. #[derive(Builder)]
#[builder(error = CError, unknown_field = "CError::WTF")]
struct TheStruct {
#[builder("self.a_default()?")]
a: i32,
#[builder("self.b_default()?")]
b: i32,
}
impl TheStructBuilder {
fn a_default() -> Result<i32, AError> { ... }
fn b_default() -> Result<i32, BError> { ... }
}
enum CError {
WTF(&'static str),
A(AError),
B(BError),
}
impl From<AError> for CError { ... } // missing this will cause a cryptic error "From<AError> is not implemented in CError. Error is inside a macro"
impl From<BError> for CError { ... } This looks like an attempt to create a new language inside of attributes. |
For the record, I feel passing a block in |
@TedDriggs This crate has version 0.9. Semver allows to break compatibility. Let's change default attribute. Users who use |
Side note: Requiring |
This test checks that we don't require unnecessary impls for custom errors when uninitialized fields are impossible.
@andy128k the macro itself doesn't - indeed, it can't - require that the specified error type have the impl; it can only attempt to use the impl and fail if it's not present. Builders with default values technically don't require this Regarding complex defaults, these are previously discussed in #53 and #65. A cursory inspection of dependent crates shows usage (example) that would be broken by attempting to enforce a serde-style more limited syntax for |
Possible option is to add additional parameter.
Downside: more changes for those who migrate from 0.9. |
Feel free to create an issue for the At this point, I believe the following are true:
As long as these hold, I'm inclined to move forward with this approach instead of the approach in #192. |
As soon as this PR removes |
@andy128k this is an interesting example of using custom validation errors today. For #181, we'd need a way for this to work without the heap allocation of
String
, which is what's driving #191.