Comment 8 for bug 1541691

Revision history for this message
Ravi Shekhar Jethani (ravishekar-jethani) wrote :

In addition to the solutions proposed another solution would be to:

Truncate the regex part of the message to 256(or something else) characters

Limit the no. of characters in ex.message(https://github.com/openstack/nova/blob/master/nova/api/validation/validators.py#L172) to 256.
All the extra characters after the limit are truncated to '...'
We can do something like:

diff --git a/nova/api/validation/validators.py b/nova/api/validation/validators.py
index 2ae5dfe..270089d 100644
--- a/nova/api/validation/validators.py
+++ b/nova/api/validation/validators.py
@@ -31,6 +31,9 @@ from nova import exception
 from nova.i18n import _

+MAX_MESSAGE_LENGTH = 256
+
+
 @jsonschema.FormatChecker.cls_checks('date-time')
 def _validate_datetime_format(instance):
     try:
@@ -163,16 +166,22 @@ class _SchemaValidator(object):
         try:
             self.validator.validate(*args, **kwargs)
         except jsonschema.ValidationError as ex:
+ msg = ex.message
+ # Error messages can be very long and cryptic because of the
+ # embeded regex pattern. So put a limit on the message length.
+ # Refer https://bugs.launchpad.net/nova/+bug/1541691
+ if len(ex.message) > MAX_MESSAGE_LENGTH:
+ msg = "{0}...".format(ex.message[:MAX_MESSAGE_LENGTH])
             # NOTE: For whole OpenStack message consistency, this error
             # message has been written as the similar format of WSME.
             if len(ex.path) > 0:
                 detail = _("Invalid input for field/attribute %(path)s."
                            " Value: %(value)s. %(message)s") % {
                                'path': ex.path.pop(), 'value': ex.instance,
- 'message': ex.message
+ 'message': msg
                            }
             else:
- detail = ex.message
+ detail = msg
             raise exception.ValidationError(detail=detail)

Example output:

$ nova boot --flavor 1 --image 21ef6ba1-1d71-42af-87a1-94216eb20217 "test "
ERROR (BadRequest): Invalid input for field/attribute name. Value: test . u'test ' does not match u'^(?![\\ \\\xa0\\\u1680\\\u180e\\\u2000\\\u2001\\\u2002\\\u2003\\\u2004\\\u2005\\\u2006\\\u2007\\\u2008\\\u2009\\\u200a\\\u202f\\\u205f\\\u3000])[\\ \\!\\"\\#\\$\\%\\&\\\'\\(\\)\\*\\+\\,\\-\\.\\/0123456789\\:\\;\\<\\=\\>\\?\\@A... (HTTP 400) (Request-ID: req-0a745169-f33e-4e13-aedd-e7160080e250)