Skip to content

Commit

Permalink
schema: print allowed length for length failures. (canonical#1056)
Browse files Browse the repository at this point in the history
LP: #1621726

Signed-off-by: Kyle Fazzari <kyle@canonical.com>
  • Loading branch information
Kyle Fazzari authored and sergiusens committed Jan 19, 2017
1 parent 2853c18 commit 6e0005e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
24 changes: 22 additions & 2 deletions snapcraft/_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

from snapcraft.internal import common

# dict of validator -> cause pairs. Wish jsonschema just gave us better
# messages.
_VALIDATION_ERROR_CAUSES = {
'maxLength': 'maximum length is {validator_value}',
'minLength': 'minimum length is {validator_value}',
}


class SnapcraftSchemaError(Exception):

Expand Down Expand Up @@ -99,7 +106,20 @@ def _handle_validation_error(error):
if path:
messages.insert(0, "The '{}' property does not match the "
"required schema:".format('/'.join(path)))
if error.cause:
messages.append('({})'.format(error.cause))
cause = error.cause or _determine_cause(error)
if cause:
messages.append('({})'.format(cause))

raise SnapcraftSchemaError(' '.join(messages))


def _determine_cause(error):
"""Attempt to determine a cause from validation error.
Returns:
A string representing the cause of the error (it may be empty if no
cause can be determined).
"""

return _VALIDATION_ERROR_CAUSES.get(error.validator, '').format(
validator_value=error.validator_value)
3 changes: 2 additions & 1 deletion snapcraft/tests/test_project_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,8 @@ def test_summary_too_long(self):

expected_message = (
"The 'summary' property does not match the required schema: "
"'{}' is too long").format(self.data['summary'])
"'{}' is too long (maximum length is 78)").format(
self.data['summary'])
self.assertEqual(raised.message, expected_message,
message=self.data)

Expand Down

0 comments on commit 6e0005e

Please sign in to comment.