装饰器 -- 函数装饰器(tornado异常响应装饰器)
生活随笔
收集整理的這篇文章主要介紹了
装饰器 -- 函数装饰器(tornado异常响应装饰器)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
裝飾器 -- 函數裝飾器(tornado異常響應裝飾器) # 值可變,每次使用需要重新賦值
ERR_RESP_TEMPLATE = {"state": "FAILED", "error": None}
RESP_TEMPLATE_4_DELETE = {"tenant_id": "", "state": "FAILED", "error": None}def _catch_except_args(err_dict=ERR_RESP_TEMPLATE):def _catch_except(func):@gen.coroutinedef wrapped_func(self, *args, **kwargs):"""wrapped function"""try:yield func(self, *args, **kwargs)except TMException as excep:_LOG.exception(str(excep))err_dict["state"] = "FAILED" # RESP_TEMPLATE_4_DELETE["state"]可能為"SUCCEED"err_dict['error'] = str(excep)self._write(200, err_dict)except Exception as excep:_LOG.exception(str(excep))self._write_error(503, SYSTEM_ERR_MSG)return wrapped_funcreturn _catch_exceptclass RequestHandlerWrapper(RequestHandler):"""RequestHandler wrapper for write response"""def _write_error(self, status_code, err_msg):"""write_error"""ERR_RESP_TEMPLATE["error"] = err_msgself._write(status_code, ERR_RESP_TEMPLATE)def _write(self, status_code, msg):"""write"""self.set_status(status_code)resp_msg = json.dumps(msg)self.write(resp_msg)_LOG.debug("<< Return response %d, %s", status_code, resp_msg)class HealthHandler(RequestHandlerWrapper):"""health check, test interface"""URL = "/api/v1/health"def initialize(self, service):"""initialize"""pass@gen.coroutinedef get(self):"""response ok"""self._write(200, {"state": "Tenant manager is ready."})class TenantHandler(RequestHandlerWrapper):"""TenantHandler"""URL = [r"/api/v1/tenants/([\w|\-|\.]*)", r"/api/v1/tenants"]def initialize(self, service):"""initialize"""self.__tenant_manager = service@_catch_except_args()@gen.coroutinedef post(self):"""post"""_LOG.info(">> Receive request for add tenant: %s", self.request.body)tenant_info_dict = json_2_dict(self.request.body)tenant_info_dict = yield self.__tenant_manager.add(tenant_info_dict)self._write(200, tenant_info_dict)@_catch_except_args(err_dict=RESP_TEMPLATE_4_DELETE)@gen.coroutinedef delete(self, tenant_id):"""delete"""_LOG.info(">> Receive request for delete tenant: %s", tenant_id)RESP_TEMPLATE_4_DELETE['tenant_id'] = tenant_idyield self.__tenant_manager.delete(tenant_id)RESP_TEMPLATE_4_DELETE['state'] = 'SUCCEED'RESP_TEMPLATE_4_DELETE['error'] = Noneself._write(200, RESP_TEMPLATE_4_DELETE)@_catch_except_args()@gen.coroutinedef put(self, tenant_id):"""put"""_LOG.info(">> Receive request for update tenant: %s, %s",tenant_id, self.request.body)tenant_info_dict = json_2_dict(self.request.body)tenant_info_dict = self.__tenant_manager.update(tenant_id, tenant_info_dict)self._write(200, tenant_info_dict)@_catch_except_args()@gen.coroutinedef get(self, tenant_id=None):"""get"""_LOG.debug(">> Receive request for get tenant(s): %s, %s",tenant_id, self.request.headers)# 當tenant_id字符串中包含非法字符時:tenant_id被u""代替substr_of_tenant_id = self.get_argument("tenant_id", None)query_condition = QueryCondition(tenant_id, substr_of_tenant_id, self.request.headers)resp_body = self.__tenant_manager.query(query_condition)self._write(200, resp_body)
?
posted on 2019-03-16 17:04 wenlin_gk 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/wenlin-gk/p/10543134.html
總結
以上是生活随笔為你收集整理的装饰器 -- 函数装饰器(tornado异常响应装饰器)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自动登录(过滤器filter的应用)
- 下一篇: [swift 进阶]读书笔记-第八章:错