Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse_request(message: str) -> Request:
"Parse a JSON string into a sub-``Request`` object."
try:
data = json_loads(message)
except JSONDecodeError as error:
raise ApiError.PARSE_ERROR('request is not valid JSON', str(error))
try:
Request(**data)
except TypeError as error:
raise ApiError.INVALID_REQUEST('request is malformed',
*error.args, None)
except ValidationError as error:
raise ApiError.INVALID_REQUEST('request is malformed',
error.json(indent=None),
getattr(data, 'get', lambda _: None)('id'))
cls = _methods.get(data['method'])
if cls:
try:
return cls(**data)
except ValidationError as error:
raise ApiError.INVALID_PARAMS('request params are invalid',
error.json(indent=None), data.get('id'))
else:
raise ApiError.METHOD_NOT_FOUND('request method not found',
None, data.get('id'))
def parse_request(message: str) -> Request:
"Parse a JSON string into a sub-``Request`` object."
try:
data = json_loads(message)
except JSONDecodeError as error:
raise ApiError.PARSE_ERROR('request is not valid JSON', str(error))
try:
Request(**data)
except TypeError as error:
raise ApiError.INVALID_REQUEST('request is malformed',
*error.args, None)
except ValidationError as error:
raise ApiError.INVALID_REQUEST('request is malformed',
error.json(indent=None),
getattr(data, 'get', lambda _: None)('id'))
cls = _methods.get(data['method'])
if cls:
try:
return cls(**data)
except ValidationError as error:
raise ApiError.INVALID_PARAMS('request params are invalid',
try:
Request(**data)
except TypeError as error:
raise ApiError.INVALID_REQUEST('request is malformed',
*error.args, None)
except ValidationError as error:
raise ApiError.INVALID_REQUEST('request is malformed',
error.json(indent=None),
getattr(data, 'get', lambda _: None)('id'))
cls = _methods.get(data['method'])
if cls:
try:
return cls(**data)
except ValidationError as error:
raise ApiError.INVALID_PARAMS('request params are invalid',
error.json(indent=None), data.get('id'))
else:
raise ApiError.METHOD_NOT_FOUND('request method not found',
None, data.get('id'))
def __getattr__(cls, name: str) -> Callable:
return partial(ApiError, ErrorCodes[name])
raise ApiError.INVALID_REQUEST('request is malformed',
*error.args, None)
except ValidationError as error:
raise ApiError.INVALID_REQUEST('request is malformed',
error.json(indent=None),
getattr(data, 'get', lambda _: None)('id'))
cls = _methods.get(data['method'])
if cls:
try:
return cls(**data)
except ValidationError as error:
raise ApiError.INVALID_PARAMS('request params are invalid',
error.json(indent=None), data.get('id'))
else:
raise ApiError.METHOD_NOT_FOUND('request method not found',
None, data.get('id'))