diff options
author | Dmitry Tantsur <dtantsur@protonmail.com> | 2022-08-30 12:15:42 +0200 |
---|---|---|
committer | Dmitry Tantsur <dtantsur@protonmail.com> | 2022-08-30 12:22:19 +0200 |
commit | 62f9c61ae6657227fd1f5dbc86c59a22b6bac28f (patch) | |
tree | 4207240060356243893dd55ef6a36e4d376a1328 /ironic | |
parent | 9f1f58c6af3aabcb3ae56e5f7b292f07e81e2864 (diff) | |
download | ironic-62f9c61ae6657227fd1f5dbc86c59a22b6bac28f.tar.gz |
Improve error message heuristics with jsonschema>=4.8
Before version 4.8, jsonschema did some wild guessing when producing
error messages for schemas with several equivalent subschemas. In
version 4.8 it is no longer done, causing error messages that are more
correct but also more generic.
This change restores guessing the potential root cause without claiming
that it's the only possible root cause. Also the traits schema is
simplified to make it less ambiguous.
See https://github.com/python-jsonschema/jsonschema/issues/991 for details.
Change-Id: Ia75cecd2bfbc602b8b2b85bdda20fdc04c5eadf4
Diffstat (limited to 'ironic')
-rw-r--r-- | ironic/api/controllers/v1/utils.py | 12 | ||||
-rwxr-xr-x | ironic/common/args.py | 17 |
2 files changed, 18 insertions, 11 deletions
diff --git a/ironic/api/controllers/v1/utils.py b/ironic/api/controllers/v1/utils.py index 04525ff65..8de2d156d 100644 --- a/ironic/api/controllers/v1/utils.py +++ b/ironic/api/controllers/v1/utils.py @@ -86,11 +86,13 @@ STANDARD_TRAITS = os_traits.get_traits() CUSTOM_TRAIT_PATTERN = "^%s[A-Z0-9_]+$" % os_traits.CUSTOM_NAMESPACE CUSTOM_TRAIT_REGEX = re.compile(CUSTOM_TRAIT_PATTERN) -TRAITS_SCHEMA = {'anyOf': [ - {'type': 'string', 'minLength': 1, 'maxLength': 255, - 'pattern': CUSTOM_TRAIT_PATTERN}, - {'type': 'string', 'enum': STANDARD_TRAITS}, -]} +TRAITS_SCHEMA = { + 'type': 'string', 'minLength': 1, 'maxLength': 255, + 'anyOf': [ + {'pattern': CUSTOM_TRAIT_PATTERN}, + {'enum': STANDARD_TRAITS}, + ] +} LOCAL_LINK_BASE_SCHEMA = { 'type': 'object', diff --git a/ironic/common/args.py b/ironic/common/args.py index 94cfe8841..bd13e3eaf 100755 --- a/ironic/common/args.py +++ b/ironic/common/args.py @@ -211,12 +211,17 @@ def _validate_schema(name, value, schema): try: jsonschema.validate(value, schema) except jsonschema.exceptions.ValidationError as e: - - # The error message includes the whole schema which can be very - # large and unhelpful, so truncate it to be brief and useful - error_msg = ' '.join(str(e).split("\n")[:3])[:-1] - raise exception.InvalidParameterValue( - _('Schema error for %s: %s') % (name, error_msg)) + error_msg = _('Schema error for %s: %s') % (name, e.message) + # Sometimes the root message is too generic, try to find a possible + # root cause: + cause = None + current = e + while current.context: + current = jsonschema.exceptions.best_match(current.context) + cause = current.message + if cause is not None: + error_msg += _('. Possible root cause: %s') % cause + raise exception.InvalidParameterValue(error_msg) return value |