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

Maybe loses error information #369

Closed
hynek opened this issue Dec 3, 2018 · 4 comments · Fixed by #411
Closed

Maybe loses error information #369

hynek opened this issue Dec 3, 2018 · 4 comments · Fixed by #411
Assignees

Comments

@hynek
Copy link

hynek commented Dec 3, 2018

One example says more than 1000 words:

n [1]: import voluptuous as v

In [2]: v.Schema(v.Maybe(v.Range(1, 2)))
Out[2]: <Schema(Any(None, Range(min=1, max=2, min_included=True, max_included=True, msg=None), msg=None), extra=PREVENT_EXTRA, required=False) object at 0x10c257828>

In [3]: v.Schema(v.Maybe(v.Range(1, 2)))(1)
Out[3]: 1

In [4]: v.Schema(v.Maybe(v.Range(1, 2)))(None)

In [5]: v.Schema(v.Maybe(v.Range(1, 2)))(3)
---------------------------------------------------------------------------
ScalarInvalid                             Traceback (most recent call last)
~/.virtualenvs/cust-ws/lib/python3.7/site-packages/voluptuous/schema_builder.py in __call__(self, data)
    266         try:
--> 267             return self._compiled([], data)
    268         except er.MultipleInvalid:

~/.virtualenvs/cust-ws/lib/python3.7/site-packages/voluptuous/validators.py in _run(self, path, value)
    203     def _run(self, path, value):
--> 204         return self._exec(self._compiled, value, path)
    205

~/.virtualenvs/cust-ws/lib/python3.7/site-packages/voluptuous/validators.py in _exec(self, funcs, v, path)
    255                 raise error if self.msg is None else AnyInvalid(
--> 256                     self.msg, path=path)
    257             raise AnyInvalid(self.msg or 'no valid value found',

~/.virtualenvs/cust-ws/lib/python3.7/site-packages/voluptuous/validators.py in _exec(self, funcs, v, path)
    248                 else:
--> 249                     return func(path, v)
    250             except Invalid as e:

~/.virtualenvs/cust-ws/lib/python3.7/site-packages/voluptuous/schema_builder.py in validate_value(path, data)
    821         if data != schema:
--> 822             raise er.ScalarInvalid('not a valid value', path)
    823         return data

ScalarInvalid: not a valid value

During handling of the above exception, another exception occurred:

MultipleInvalid                           Traceback (most recent call last)
<ipython-input-5-cbbab77861f1> in <module>
----> 1 v.Schema(v.Maybe(v.Range(1, 2)))(3)

~/.virtualenvs/cust-ws/lib/python3.7/site-packages/voluptuous/schema_builder.py in __call__(self, data)
    269             raise
    270         except er.Invalid as e:
--> 271             raise er.MultipleInvalid([e])
    272             # return self.validate([], self.schema, data)
    273

MultipleInvalid: not a valid value

I would like to supply a good error message to the user but setting msg on Maybe doesn't work and Range's gets ignored.

svisser added a commit to svisser/voluptuous that referenced this issue Dec 14, 2018
This makes it possible to supply a good error message to the user.
This got reported in alecthomas#369
@svisser
Copy link
Collaborator

svisser commented Dec 15, 2018

@hynek thanks for reporting this! There is now the ability to set msg on Maybe in the master branch: 43ea94b. I don't know when this will be available in a released version of voluptuous though.

Do you think this change is sufficient for resolving this issue or should we explore further functionality that uses any msg set on the validator that's passed to Maybe (not sure how easy this is though)?

@svisser svisser self-assigned this Dec 15, 2018
@hynek
Copy link
Author

hynek commented Dec 17, 2018

Is there a precedent of such composites bubbling up messages? I think I could live with just setting msg on the Maybe myself.

@svisser
Copy link
Collaborator

svisser commented Aug 21, 2019

@hynek just a quick message to say that a new version of voluptuous was released recently with the above change.

@hynek
Copy link
Author

hynek commented Aug 21, 2019

Thanks for letting me know and for keeping this great library alive!

monopolis added a commit to monopolis/voluptuous that referenced this issue Nov 6, 2019
Due to Any evaluating all options, and raising the exception
from the first encountered error, Maybe would discard the
expected error raised by the provided validator.

This commit changes the order of the validators in Maybe so
that the first evaluated error (and thus returned) is that of
the provided validator.

Change-Id: I03bc182dff90f752a882e5920a034458bd8226b5
monopolis added a commit to monopolis/voluptuous that referenced this issue Nov 7, 2019
Due to Any evaluating all options, and raising the exception
from the first encountered error, Maybe would discard the
expected error raised by the provided validator and only
raise a invalid value error (due to values not being None).

This commit changes the order of the validators in Maybe so
that the first evaluated error, and thus returned is that of
the provided validator.
monopolis added a commit to monopolis/voluptuous that referenced this issue Nov 29, 2019
Due to Any evaluating all options, and raising the exception
from the first encountered error, Maybe would discard the
expected error raised by the provided validator and only
raise a invalid value error (due to values not being None).

This commit changes the order of the validators in Maybe so
that the first evaluated error, and thus returned is that of
the provided validator.
monopolis added a commit to monopolis/voluptuous that referenced this issue Dec 16, 2019
Due to Any evaluating all options, and raising the exception
from the first encountered error, Maybe would discard the
expected error raised by the provided validator and only
raise a invalid value error (due to values not being None).

This commit changes the order of the validators in Maybe so
that the first evaluated error, and thus returned is that of
the provided validator.
monopolis added a commit to monopolis/voluptuous that referenced this issue Oct 12, 2020
Due to Any evaluating all options, and raising the exception
from the first encountered error, Maybe would discard the
expected error raised by the provided validator and only
raise a invalid value error (due to values not being None).

This commit changes the order of the validators in Maybe so
that the first evaluated error, and thus returned is that of
the provided validator.
@spacegaier spacegaier linked a pull request Dec 6, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants