summaryrefslogtreecommitdiffstats
path: root/testapi/opnfv_testapi/resources
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2017-04-05 16:23:03 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2017-04-06 16:55:21 +0800
commit0620b2cbb21714c1a5e485186c46c2c24485ef37 (patch)
tree48fae29c4a2827d51ce6bc418910194b54d35f5c /testapi/opnfv_testapi/resources
parentdbdb452b2761a630ec8574b22553ac5531611201 (diff)
unify error message in TestAPI
Change-Id: I994feb7bf340c9e48bebe9fdf3dc3a76bc254652 Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'testapi/opnfv_testapi/resources')
-rw-r--r--testapi/opnfv_testapi/resources/handlers.py31
-rw-r--r--testapi/opnfv_testapi/resources/pod_handlers.py4
-rw-r--r--testapi/opnfv_testapi/resources/project_handlers.py4
-rw-r--r--testapi/opnfv_testapi/resources/result_handlers.py15
-rw-r--r--testapi/opnfv_testapi/resources/scenario_handlers.py6
-rw-r--r--testapi/opnfv_testapi/resources/testcase_handlers.py9
6 files changed, 32 insertions, 37 deletions
diff --git a/testapi/opnfv_testapi/resources/handlers.py b/testapi/opnfv_testapi/resources/handlers.py
index c2b1a64..522bbe7 100644
--- a/testapi/opnfv_testapi/resources/handlers.py
+++ b/testapi/opnfv_testapi/resources/handlers.py
@@ -28,6 +28,7 @@ from tornado import gen
from tornado import web
import models
+from opnfv_testapi.common import message
from opnfv_testapi.common import raises
from opnfv_testapi.tornado_swagger import swagger
@@ -56,7 +57,7 @@ class GenericApiHandler(web.RequestHandler):
try:
self.json_args = json.loads(self.request.body)
except (ValueError, KeyError, TypeError) as error:
- raises.BadRequest("Bad Json format [{}]".format(error))
+ raises.BadRequest(message.bad_format(str(error)))
def finish_request(self, json_object=None):
if json_object:
@@ -81,11 +82,11 @@ class GenericApiHandler(web.RequestHandler):
try:
token = self.request.headers['X-Auth-Token']
except KeyError:
- raises.Unauthorized("No Authentication Header.")
+ raises.Unauthorized(message.unauthorized())
query = {'access_token': token}
check = yield self._eval_db_find_one(query, 'tokens')
if not check:
- raises.Forbidden("Invalid Token.")
+ raises.Forbidden(message.invalid_token())
ret = yield gen.coroutine(method)(self, *args, **kwargs)
raise gen.Return(ret)
return wrapper
@@ -97,13 +98,13 @@ class GenericApiHandler(web.RequestHandler):
:param db_checks: [(table, exist, query, error)]
"""
if self.json_args is None:
- raises.BadRequest('no body')
+ raises.BadRequest(message.no_body())
data = self.table_cls.from_dict(self.json_args)
for miss in miss_checks:
miss_data = data.__getattribute__(miss)
if miss_data is None or miss_data == '':
- raises.BadRequest('{} missing'.format(miss))
+ raises.BadRequest(message.missing(miss))
for k, v in kwargs.iteritems():
data.__setattr__(k, v)
@@ -111,8 +112,8 @@ class GenericApiHandler(web.RequestHandler):
for table, exist, query, error in db_checks:
check = yield self._eval_db_find_one(query(data), table)
if (exist and not check) or (not exist and check):
- code, message = error(data)
- raises.CodeTBD(code, message)
+ code, msg = error(data)
+ raises.CodeTBD(code, msg)
if self.table != 'results':
data.creation_date = datetime.now()
@@ -148,16 +149,14 @@ class GenericApiHandler(web.RequestHandler):
def _get_one(self, query):
data = yield self._eval_db_find_one(query)
if data is None:
- raises.NotFound("[{}] not exist in table [{}]"
- .format(query, self.table))
+ raises.NotFound(message.not_found(self.table, query))
self.finish_request(self.format_data(data))
@authenticate
def _delete(self, query):
data = yield self._eval_db_find_one(query)
if data is None:
- raises.NotFound("[{}] not exit in table [{}]"
- .format(query, self.table))
+ raises.NotFound(message.not_found(self.table, query))
yield self._eval_db(self.table, 'remove', query)
self.finish_request()
@@ -165,13 +164,12 @@ class GenericApiHandler(web.RequestHandler):
@authenticate
def _update(self, query, db_keys):
if self.json_args is None:
- raises.BadRequest("No payload")
+ raises.BadRequest(message.no_body())
# check old data exist
from_data = yield self._eval_db_find_one(query)
if from_data is None:
- raises.NotFound("{} could not be found in table [{}]"
- .format(query, self.table))
+ raises.NotFound(message.not_found(self.table, query))
data = self.table_cls.from_dict(from_data)
# check new data exist
@@ -179,8 +177,7 @@ class GenericApiHandler(web.RequestHandler):
if not equal:
to_data = yield self._eval_db_find_one(new_query)
if to_data is not None:
- raises.Forbidden("{} already exists in table [{}]"
- .format(new_query, self.table))
+ raises.Forbidden(message.exist(self.table, new_query))
# we merge the whole document """
edit_request = self._update_requests(data)
@@ -197,7 +194,7 @@ class GenericApiHandler(web.RequestHandler):
request = self._update_request(request, k, v,
data.__getattribute__(k))
if not request:
- raises.Forbidden("Nothing to update")
+ raises.Forbidden(message.no_update())
edit_request = data.format()
edit_request.update(request)
diff --git a/testapi/opnfv_testapi/resources/pod_handlers.py b/testapi/opnfv_testapi/resources/pod_handlers.py
index fd9ce3e..2c303c9 100644
--- a/testapi/opnfv_testapi/resources/pod_handlers.py
+++ b/testapi/opnfv_testapi/resources/pod_handlers.py
@@ -9,6 +9,7 @@
import httplib
import handlers
+from opnfv_testapi.common import message
from opnfv_testapi.tornado_swagger import swagger
import pod_models
@@ -46,8 +47,7 @@ class PodCLHandler(GenericPodHandler):
return {'name': data.name}
def error(data):
- message = '{} already exists as a pod'.format(data.name)
- return httplib.FORBIDDEN, message
+ return httplib.FORBIDDEN, message.exist('pod', data.name)
miss_checks = ['name']
db_checks = [(self.table, False, query, error)]
diff --git a/testapi/opnfv_testapi/resources/project_handlers.py b/testapi/opnfv_testapi/resources/project_handlers.py
index 087bb8a..59e0b88 100644
--- a/testapi/opnfv_testapi/resources/project_handlers.py
+++ b/testapi/opnfv_testapi/resources/project_handlers.py
@@ -9,6 +9,7 @@
import httplib
import handlers
+from opnfv_testapi.common import message
from opnfv_testapi.tornado_swagger import swagger
import project_models
@@ -48,8 +49,7 @@ class ProjectCLHandler(GenericProjectHandler):
return {'name': data.name}
def error(data):
- message = '{} already exists as a project'.format(data.name)
- return httplib.FORBIDDEN, message
+ return httplib.FORBIDDEN, message.exist('project', data.name)
miss_checks = ['name']
db_checks = [(self.table, False, query, error)]
diff --git a/testapi/opnfv_testapi/resources/result_handlers.py b/testapi/opnfv_testapi/resources/result_handlers.py
index 3e78057..609c5ce 100644
--- a/testapi/opnfv_testapi/resources/result_handlers.py
+++ b/testapi/opnfv_testapi/resources/result_handlers.py
@@ -12,6 +12,7 @@ import httplib
from bson import objectid
+from opnfv_testapi.common import message
from opnfv_testapi.common import raises
from opnfv_testapi.resources import handlers
from opnfv_testapi.resources import result_models
@@ -30,7 +31,7 @@ class GenericResultHandler(handlers.GenericApiHandler):
try:
value = int(value)
except:
- raises.BadRequest('{} must be int'.format(key))
+ raises.BadRequest(message.must_int(key))
return value
def set_query(self):
@@ -144,23 +145,21 @@ class ResultsCLHandler(GenericResultHandler):
return {'name': data.pod_name}
def pod_error(data):
- message = 'Could not find pod [{}]'.format(data.pod_name)
- return httplib.NOT_FOUND, message
+ return httplib.NOT_FOUND, message.not_found('pod', data.pod_name)
def project_query(data):
return {'name': data.project_name}
def project_error(data):
- message = 'Could not find project [{}]'.format(data.project_name)
- return httplib.NOT_FOUND, message
+ return httplib.NOT_FOUND, message.not_found('project',
+ data.project_name)
def testcase_query(data):
return {'project_name': data.project_name, 'name': data.case_name}
def testcase_error(data):
- message = 'Could not find testcase [{}] in project [{}]'\
- .format(data.case_name, data.project_name)
- return httplib.NOT_FOUND, message
+ return httplib.NOT_FOUND, message.not_found('testcase',
+ data.case_name)
miss_checks = ['pod_name', 'project_name', 'case_name']
db_checks = [('pods', True, pod_query, pod_error),
diff --git a/testapi/opnfv_testapi/resources/scenario_handlers.py b/testapi/opnfv_testapi/resources/scenario_handlers.py
index 9d0233c..bad79fd 100644
--- a/testapi/opnfv_testapi/resources/scenario_handlers.py
+++ b/testapi/opnfv_testapi/resources/scenario_handlers.py
@@ -1,6 +1,7 @@
import functools
import httplib
+from opnfv_testapi.common import message
from opnfv_testapi.common import raises
from opnfv_testapi.resources import handlers
import opnfv_testapi.resources.scenario_models as models
@@ -82,8 +83,7 @@ class ScenariosCLHandler(GenericScenarioHandler):
return {'name': data.name}
def error(data):
- message = '{} already exists as a scenario'.format(data.name)
- return httplib.FORBIDDEN, message
+ return httplib.FORBIDDEN, message.exist('scenario', data.name)
miss_checks = ['name']
db_checks = [(self.table, False, query, error)]
@@ -184,7 +184,7 @@ class ScenarioGURHandler(GenericScenarioHandler):
def _update_requests_rename(self, data):
data.name = self._term.get('name')
if not data.name:
- raises.BadRequest("new scenario name is not provided")
+ raises.BadRequest(message.missing('name'))
def _update_requests_add_installer(self, data):
data.installers.append(models.ScenarioInstaller.from_dict(self._term))
diff --git a/testapi/opnfv_testapi/resources/testcase_handlers.py b/testapi/opnfv_testapi/resources/testcase_handlers.py
index 1211a05..bc22b74 100644
--- a/testapi/opnfv_testapi/resources/testcase_handlers.py
+++ b/testapi/opnfv_testapi/resources/testcase_handlers.py
@@ -8,6 +8,7 @@
##############################################################################
import httplib
+from opnfv_testapi.common import message
from opnfv_testapi.resources import handlers
from opnfv_testapi.resources import testcase_models
from opnfv_testapi.tornado_swagger import swagger
@@ -58,13 +59,11 @@ class TestcaseCLHandler(GenericTestcaseHandler):
}
def p_error(data):
- message = 'Could not find project [{}]'.format(data.project_name)
- return httplib.FORBIDDEN, message
+ return httplib.FORBIDDEN, message.not_found('project',
+ data.project_name)
def tc_error(data):
- message = '{} already exists as a testcase in project {}'\
- .format(data.name, data.project_name)
- return httplib.FORBIDDEN, message
+ return httplib.FORBIDDEN, message.exist('testcase', data.name)
miss_checks = ['name']
db_checks = [(self.db_projects, True, p_query, p_error),