Fix exception handling in _alarm_definition_create

Use oslo encodeutils to safely format exception title and description
both in Python 2 and 3. Otherwise exception is thrown[1]:

  TypeError: b'Invalid alarm expression' is not JSON serializable

[1] http://logs.openstack.org/58/606658/2/check/openstack-tox-lower-constraints/0a33141/testr_results.html.gz

Story: 2003240
Task: 26920

Change-Id: I78ca6f43a4d998613d4c3f64f82a384701d620dd
This commit is contained in:
Witold Bedyk 2018-10-05 14:05:12 +02:00
parent 01740e584b
commit 11e5bceffd
1 changed files with 5 additions and 4 deletions

View File

@ -502,10 +502,11 @@ class AlarmDefinitions(alarm_definitions_api_v2.AlarmDefinitionsV2API,
except (pyparsing.ParseException,
pyparsing.ParseFatalException) as ex:
LOG.exception(ex)
title = "Invalid alarm expression".encode('utf8')
msg = "parser failed on expression '{}' at column {}: {}".format(
expression.encode('utf8'), str(ex.column).encode('utf8'),
ex.msg.encode('utf8'))
title = u"Invalid alarm expression"
msg = u"parser failed on expression '{}' at column {}: {}".format(
encodeutils.safe_decode(expression, 'utf-8'),
encodeutils.safe_decode(str(ex.column), 'utf-8'),
encodeutils.safe_decode(ex.msg, 'utf-8'))
raise HTTPUnprocessableEntityError(title, msg)
self._validate_name_not_conflicting(tenant_id, name)