diff --git a/src/bepasty/apis/rest.py b/src/bepasty/apis/rest.py index e9cb34f9..6693f56f 100644 --- a/src/bepasty/apis/rest.py +++ b/src/bepasty/apis/rest.py @@ -26,17 +26,22 @@ # "/lodgeit/" in the same blueprint and there is no way to exclude # from using the same error handler ("/lodgeit/" is not REST api). def rest_errorhandler(func): + def error_message(description, code): + return jsonify({ + 'error': {'code': code, 'message': description}, + }), code + def handler(*args, **kwargs): try: return func(*args, **kwargs) except HTTPException as exc: - return exc.description, exc.code + return error_message(exc.description, exc.code) except Exception: if current_app.propagate_exceptions: # if testing/debug mode, re-raise raise exc = InternalServerError() - return exc.description, exc.code + return error_message(exc.description, exc.code) return handler @@ -79,6 +84,8 @@ def update_item(self, item, name): # Make a Response and create Transaction-ID from ItemName response = make_response() + response.headers['Content-Type'] = 'application/json' + response.data = '{}' name_b = name if isinstance(name, bytes_type) else name.encode() trans_id_b = base64.b64encode(name_b) trans_id_s = trans_id_b if isinstance(trans_id_b, str) else trans_id_b.decode() diff --git a/src/bepasty/tests/test_rest_server.py b/src/bepasty/tests/test_rest_server.py index 2dddcfb7..66c66ccd 100644 --- a/src/bepasty/tests/test_rest_server.py +++ b/src/bepasty/tests/test_rest_server.py @@ -160,7 +160,9 @@ def check_response(response, code, ftype='application/json', check_data=True): def check_err_response(response, code, check_data=True): - check_response(response, code, 'text/html; charset=utf-8', check_data) + check_response(response, code, check_data=check_data) + if check_data: + assert code == response.json['error']['code'] # check if doesn't have html tag assert not re.match(r'<.+>', response.data.decode()) @@ -183,14 +185,14 @@ def check_data_response(response, meta, data, offset=0, total_size=None, assert data == response.data -def check_json_response(response, metas, check_data=True): - check_response(response, 200, check_data=check_data) +def check_json_response(response, metas, code=200, check_data=True): + check_response(response, code, check_data=check_data) if check_data: assert metas == response.json def check_upload_response(response, code=201, check_data=True): - check_response(response, code, 'text/html; charset=utf-8', check_data) + check_json_response(response, {}, code=code, check_data=check_data) if code == 200: assert len(response.headers[TRANSACTION_ID]) > 0