Skip to content

Conversation

@arianvp
Copy link

@arianvp arianvp commented Jul 24, 2015

When reading the documentation and reading the source code of jsonschema
I found an inconsitency in the code.

Basically, when a FormatError occurs, it's re-raised into a
ValidationError with its cause set to the original FormatError according
to the documentation. Which makes a lot of sense.

But in the code, the cause of the ValidationError is set to the cause of
the FormatError. Which is not the documented behaviour nor very useful
(in my opinion); So I consider this as a bug.

See the documentation here:
https://python-jsonschema.readthedocs.org/en/latest/errors/#jsonschema.exceptions.ValidationError.cause

When reading the documentation and reading the source code of jsonschema I found an inconsitency in the code. Basically, when a FormatError occurs, it's re-raised into a ValidationError with its cause set to the original FormatError according to the documentation. Which makes a lot of sense. But in the code, the cause of the ValidationError is set to the cause of the FormatError. Which is not the documented behaviour nor very useful (in my opinion); So I consider this as a bug. See the documentation here: https://python-jsonschema.readthedocs.org/en/latest/errors/#jsonschema.exceptions.ValidationError.cause
@Julian
Copy link
Member

Julian commented Jul 24, 2015

Hi! Thanks.

Can you clarify why you think this isn't useful? What is it you want off of a FormatError object?

@arianvp
Copy link
Author

arianvp commented Jul 25, 2015

Well i want to be able to tell if an error happened due to the validator failing or due to a format checker failing. The docs say I can do this with e.cause, but because we create a new ValidationError and set its cause to format_error.cause, ValidationError's cause will always be None because usually FormatErrors don't have cause set

@arianvp
Copy link
Author

arianvp commented Jul 25, 2015

So I think the ValidationError should have its cause set to the FormatError

@Julian
Copy link
Member

Julian commented Jul 25, 2015

Which validator?

Do you just mean you want if error.validator != "format"? cause is supposed to mean whatever non-validation error caused the failure, e.g. if you used a hostname format then it will be some ValueError that was raised with information about why the hostname is invalid. FormatErrors should generally have cause set, see the tests that fail after this change is made.

If you have a case where one isn't being set I'd love to help, could you provide some details on your instance and schema?

@arianvp
Copy link
Author

arianvp commented Jul 27, 2015

So a FormatError is not considered a non-validation error then? I thought the docs meant: "Because FormatErrors that can occur in formatters are not ValidationErrors, we return a ValidationError in the format validator with its cause set to FormatError.

The use-case we had was: We wanted to print the reason of a failed schema check only if a format error occured. Namely, we used a formatter to check if a password string was valid, and instead of leaking the password back to the user or into the logs, we wanted to do a custom error message instead.

How would I do that in this case? check if error.validator != 'format' ?

@Julian
Copy link
Member

Julian commented Jul 27, 2015

FormatError is just a part of the format API that rolls up other Exceptions
for Validators so that they can not know what specifically they need to
catch. It'd be private except that you can implement custom formats.

But otherwise there's nothing useful for library users in them. The docs
mean "with the cause being whatever the actual exception was, which was
something before the format error".

Presumably yes, you'd just want

message = error.message if error.validator != " format" or
error.schema_path[-1] != "password" else "Invalid Password"
On Jul 27, 2015 09:16, "Arian van Putten" notifications@github.com wrote:

So a FormatError is not considered a non-validation error then? I thought
the docs meant: "Because FormatErrors that can occur in formatters are
not ValidationErrors, we return a ValidationError in the format validator
with its cause set to FormatError.

The use-case we had was: We wanted to print the reason of a failed schema
check only if a format error occured. Namely, we used a formatter to check
if a password string was valid, and instead of leaking the password back to
the user or into the logs, we wanted to do a custom error message instead.

How would I do that in this case? check if error.validator != 'format' ?


Reply to this email directly or view it on GitHub
#242 (comment).

@arianvp
Copy link
Author

arianvp commented Jul 27, 2015

Alright thanks. That makes sense. I guess we can close the pull request then

@Julian
Copy link
Member

Julian commented Jul 27, 2015

Cool, lemme know if you have any other questions.

@Julian Julian closed this Jul 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants