Commit 9b38d111 by Sheng

Use class attribute formater

parent b69bdefc
...@@ -44,6 +44,8 @@ class InvalidException(Exception): ...@@ -44,6 +44,8 @@ class InvalidException(Exception):
class MixinHandler(object): class MixinHandler(object):
formater = 'Missing value {}'
def write_error(self, status_code, **kwargs): def write_error(self, status_code, **kwargs):
exc_info = kwargs.get('exc_info') exc_info = kwargs.get('exc_info')
if exc_info and len(exc_info) > 1: if exc_info and len(exc_info) > 1:
...@@ -52,10 +54,10 @@ class MixinHandler(object): ...@@ -52,10 +54,10 @@ class MixinHandler(object):
self._reason = info.split(':', 1)[-1].strip() self._reason = info.split(':', 1)[-1].strip()
super(MixinHandler, self).write_error(status_code, **kwargs) super(MixinHandler, self).write_error(status_code, **kwargs)
def get_value(self, name, formater='Missing value {}'): def get_value(self, name):
value = self.get_argument(name) value = self.get_argument(name)
if not value: if not value:
raise InvalidException(formater.format(name)) raise InvalidException(self.formater.format(name))
return value return value
def get_real_client_addr(self): def get_real_client_addr(self):
...@@ -262,7 +264,7 @@ class WsockHandler(MixinHandler, tornado.websocket.WebSocketHandler): ...@@ -262,7 +264,7 @@ class WsockHandler(MixinHandler, tornado.websocket.WebSocketHandler):
self.src_addr = self.get_client_addr() self.src_addr = self.get_client_addr()
logging.info('Connected from {}:{}'.format(*self.src_addr)) logging.info('Connected from {}:{}'.format(*self.src_addr))
try: try:
worker_id = self.get_value('id', formater=self.formater) worker_id = self.get_value('id')
except (tornado.web.MissingArgumentError, InvalidException) as exc: except (tornado.web.MissingArgumentError, InvalidException) as exc:
self.close(reason=str(exc).split(':', 1)[-1].strip()) self.close(reason=str(exc).split(':', 1)[-1].strip())
else: else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment