fix: 'Error' class
This commit is contained in:
@@ -33,18 +33,18 @@ class Request[T]:
|
||||
varnames = handler.__code__.co_varnames
|
||||
|
||||
if self.params is None:
|
||||
if argcount > 0: raise self.ParamsError()
|
||||
if argcount > 0: raise self.ParamsError(None)
|
||||
return handler()
|
||||
|
||||
if isinstance(args, list):
|
||||
if len(args) != argcount: raise self.ParamsError()
|
||||
if len(args) != argcount: raise self.ParamsError(args)
|
||||
return handler(*args)
|
||||
|
||||
if isinstance(args, dict):
|
||||
if args.keys() != set(varnames): raise self.ParamsError()
|
||||
if args.keys() != set(varnames): raise self.ParamsError(args)
|
||||
return handler(**args)
|
||||
|
||||
raise TypeError()
|
||||
raise TypeError(repr(args))
|
||||
|
||||
class Response[T]:
|
||||
def __init__(self, id: str|int, inner: T):
|
||||
@@ -60,23 +60,24 @@ class Response[T]:
|
||||
data['result'] = self.inner
|
||||
return json.dumps(data)
|
||||
|
||||
class Error(Enum):
|
||||
PARSE_ERROR = -32700
|
||||
INVALID_REQUEST = -32600
|
||||
METHOD_NOT_FOUND = -32601
|
||||
INVALID_PARAMS = -32602
|
||||
INTERNAL_ERROR = -32603
|
||||
class Error:
|
||||
def __init__(self, code, data=None):
|
||||
self.code: Error.Code = code
|
||||
self.data = data
|
||||
|
||||
def __init__(self, *args):
|
||||
super().__init__(*args)
|
||||
self.data = None
|
||||
class Code(Enum):
|
||||
PARSE_ERROR = -32700
|
||||
INVALID_REQUEST = -32600
|
||||
METHOD_NOT_FOUND = -32601
|
||||
INVALID_PARAMS = -32602
|
||||
INTERNAL_ERROR = -32603
|
||||
|
||||
def message(self) -> str:
|
||||
return self.name.capitalize().replace('_', ' ')
|
||||
return self.code.name.capitalize().replace('_', ' ')
|
||||
|
||||
def response(self):
|
||||
result = dict()
|
||||
result['code'] = self.value
|
||||
result['code'] = self.code.value
|
||||
result['message'] = self.message()
|
||||
if self.data is not None:
|
||||
result['data'] = self.data
|
||||
@@ -123,23 +124,23 @@ async def handler(request: ws.WebSocketRequest):
|
||||
mid = inbound.id
|
||||
res = inbound.fulfill()
|
||||
except json.decoder.JSONDecodeError as e:
|
||||
err = Error.PARSE_ERROR
|
||||
err = Error(Error.Code.PARSE_ERROR)
|
||||
exc = e
|
||||
except Request.ParamsError as e:
|
||||
err = Error.INVALID_PARAMS
|
||||
err = Error(Error.Code.INVALID_PARAMS)
|
||||
exc = e
|
||||
except TypeError as e:
|
||||
err = Error.INVALID_REQUEST
|
||||
err = Error(Error.Code.INVALID_REQUEST)
|
||||
exc = e
|
||||
except KeyError as e:
|
||||
err = Error.METHOD_NOT_FOUND
|
||||
err = Error(Error.Code.METHOD_NOT_FOUND)
|
||||
err.data = str(e).strip("'")
|
||||
exc = e
|
||||
except ws.ConnectionClosed as e:
|
||||
logger.critical('Going away', exc_info=e)
|
||||
return
|
||||
except Exception as e:
|
||||
err = Error.INTERNAL_ERROR
|
||||
err = Error(Error.Code.INTERNAL_ERROR)
|
||||
exc = e
|
||||
|
||||
if err is not None: logger.error(err.message(), exc_info=exc)
|
||||
|
||||
Reference in New Issue
Block a user