From a9f4308bb770c3cf1d0f521b876941463273d248 Mon Sep 17 00:00:00 2001 From: LV-426 Date: Mon, 30 Mar 2015 18:27:01 +0300 Subject: common_rest_req_exception_handler(): - allow request handling to raise exceptions - call proper response builder according to exception --- src/server/rz_req_handling.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/server') diff --git a/src/server/rz_req_handling.py b/src/server/rz_req_handling.py index 2960874c..4be9d553 100644 --- a/src/server/rz_req_handling.py +++ b/src/server/rz_req_handling.py @@ -46,6 +46,24 @@ def common_resp_handle__client_error(data=None, error=None, status=400): def common_resp_handle__server_error(data=None, error=None, status=500): return __common_resp_handle(data, error, status) +def common_rest_req_exception_handler(rest_API_endpoint): + + @wraps(rest_API_endpoint) + def rest_API_endpoint__decorated(*args, **kwargs): + try: + return rest_API_endpoint(*args, **kwargs) + except API_Exception__bad_request as e: + log.exception(e) + return common_resp_handle__client_error(error=e) # currently blame client for all DNFs + except RZDoc_Exception__not_found as e: + log.exception(e) + return common_resp_handle__client_error(error=e) # currently blame client for all DNFs + except Exception as e: + log.exception(e) + return common_resp_handle__server_error(error=e) + + return rest_API_endpoint__decorated + def make_response__json(status=200, data={}): """ Construct a json response with proper content-type header -- cgit v1.3.1