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

Implement core::error::Error for ErrorIterator #451

Open
tgross35 opened this issue Feb 5, 2024 · 1 comment
Open

Implement core::error::Error for ErrorIterator #451

tgross35 opened this issue Feb 5, 2024 · 1 comment

Comments

@tgross35
Copy link

tgross35 commented Feb 5, 2024

To do this ErrorIterator needs a Display impl, which could require making it a wrapper over Box<dyn Iterator<Item = ValidationError<'a>> + Sync + Send + 'a>, rather than just a type alias (a mild breaking change).

This will allow throwing this error to dyn Error or anyhow without any manual mapping. This impl should probably just print a list of all errors, like the examples show. Roughly:

type ErrorIterTy<'a> = Box<dyn Iterator<Item = ValidationError<'a>> + Sync + Send + 'a>;
pub struct ErrorIterator<'a>(ErrorIterTy<'a>);

impl<'a> IntoIterator for ErrorIterator<'a> {
    type Item = ValidationError<'a>;
    type IntoIter: ErrorIterTy<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.0
    }
}

impl fmt::Display for ErrorIterator {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        writeln!(f, "Validation errors:");
        for (idx, e) in self.enumerate() {
            writeln!(f, "{idx:02}: {e}")?;
        }
        Ok(())
    }
}

impl core::error::Error for ErrorIterator {}
@Stranger6667
Copy link
Owner

Hey, sorry for the delay!

First of all, thank you for opening this issue, it has been unaddressed way too long.

My inclination is to adjust the API so this type is only returned by a new iter_errors function, and validate will return just the first error. I believe it will make the API more consistent with other libraries in the JSON Schema ecosystem (e.g. Python's jsonschema has it + Python bindings to this crate already have it). Even though it is a significant breaking change, I think it should be addressed before 1.0.

Other than that, I completely agree with the proposed changes and will address it in the next couple of releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants