Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _handle_request(self, msg_id, method_name, params):
"""Handles a request from the client."""
try:
handler = self._get_handler(method_name)
# workspace/executeCommand is a special case
if method_name == WORKSPACE_EXECUTE_COMMAND:
handler(params, msg_id)
else:
self._execute_request(msg_id, handler, params)
except JsonRpcException as e:
logger.exception('Failed to handle request {} {} {}'
.format(msg_id, method_name, params))
self._send_response(msg_id, None, e.to_dict())
except Exception:
logger.exception('Failed to handle request {} {} {}'
.format(msg_id, method_name, params))
err = JsonRpcInternalError.of(sys.exc_info()).to_dict()
self._send_response(msg_id, None, err)
class JsonRpcInternalError(JsonRpcException):
CODE = -32602
MESSAGE = 'Internal Error'
@classmethod
def of(cls, exc_info):
exc_type, exc_value, exc_tb = exc_info
return cls(
message=''.join(traceback.format_exception_only(
exc_type, exc_value)).strip(),
data={'traceback': traceback.format_tb(exc_tb)}
)
class JsonRpcInvalidParams(JsonRpcException):
CODE = -32602
MESSAGE = 'Invalid Params'
class JsonRpcInvalidRequest(JsonRpcException):
CODE = -32600
MESSAGE = 'Invalid Request'
class JsonRpcMethodNotFound(JsonRpcException):
CODE = -32601
MESSAGE = 'Method Not Found'
@classmethod
def of(cls, method):
return cls(message=cls.MESSAGE + ': ' + method)
def to_dict(self):
exception_dict = {
'code': self.code,
'message': self.message,
}
if self.data is not None:
exception_dict['data'] = self.data
return exception_dict
class JsonRpcRequestCancelled(JsonRpcException):
CODE = -32800
MESSAGE = 'Request Cancelled'
class JsonRpcInternalError(JsonRpcException):
CODE = -32602
MESSAGE = 'Internal Error'
@classmethod
def of(cls, exc_info):
exc_type, exc_value, exc_tb = exc_info
return cls(
message=''.join(traceback.format_exception_only(
exc_type, exc_value)).strip(),
data={'traceback': traceback.format_tb(exc_tb)}
)
class JsonRpcInvalidParams(JsonRpcException):
CODE = -32602
MESSAGE = 'Invalid Params'
def from_dict(error):
for exc_class in _EXCEPTIONS:
if exc_class.supports_code(error['code']):
return exc_class(**error)
return JsonRpcException(**error)
exc_type, exc_value)).strip(),
data={'traceback': traceback.format_tb(exc_tb)}
)
class JsonRpcInvalidParams(JsonRpcException):
CODE = -32602
MESSAGE = 'Invalid Params'
class JsonRpcInvalidRequest(JsonRpcException):
CODE = -32600
MESSAGE = 'Invalid Request'
class JsonRpcMethodNotFound(JsonRpcException):
CODE = -32601
MESSAGE = 'Method Not Found'
@classmethod
def of(cls, method):
return cls(message=cls.MESSAGE + ': ' + method)
class JsonRpcParseError(JsonRpcException):
CODE = -32700
MESSAGE = 'Parse Error'
class JsonRpcServerError(JsonRpcException):
def __init__(self, message, code, data=None):
class JsonRpcMethodNotFound(JsonRpcException):
CODE = -32601
MESSAGE = 'Method Not Found'
@classmethod
def of(cls, method):
return cls(message=cls.MESSAGE + ': ' + method)
class JsonRpcParseError(JsonRpcException):
CODE = -32700
MESSAGE = 'Parse Error'
class JsonRpcRequestCancelled(JsonRpcException):
CODE = -32800
MESSAGE = 'Request Cancelled'
class JsonRpcServerError(JsonRpcException):
def __init__(self, message, code, data=None):
if not _is_server_error_code(code):
raise ValueError('Error code should be in range -32099 - -32000')
super().__init__(
message=message, code=code, data=data)
@classmethod
def supports_code(cls, code):
return _is_server_error_code(code)
class JsonRpcInvalidRequest(JsonRpcException):
CODE = -32600
MESSAGE = 'Invalid Request'
class JsonRpcMethodNotFound(JsonRpcException):
CODE = -32601
MESSAGE = 'Method Not Found'
@classmethod
def of(cls, method):
return cls(message=cls.MESSAGE + ': ' + method)
class JsonRpcParseError(JsonRpcException):
CODE = -32700
MESSAGE = 'Parse Error'
class JsonRpcRequestCancelled(JsonRpcException):
CODE = -32800
MESSAGE = 'Request Cancelled'
class JsonRpcServerError(JsonRpcException):
def __init__(self, message, code, data=None):
if not _is_server_error_code(code):
raise ValueError('Error code should be in range -32099 - -32000')
super().__init__(
message=message, code=code, data=data)
@classmethod
def of(cls, method):
return cls(message=cls.MESSAGE + ': ' + method)
class JsonRpcParseError(JsonRpcException):
CODE = -32700
MESSAGE = 'Parse Error'
class JsonRpcRequestCancelled(JsonRpcException):
CODE = -32800
MESSAGE = 'Request Cancelled'
class JsonRpcServerError(JsonRpcException):
def __init__(self, message, code, data=None):
if not _is_server_error_code(code):
raise ValueError('Error code should be in range -32099 - -32000')
super().__init__(
message=message, code=code, data=data)
@classmethod
def supports_code(cls, code):
return _is_server_error_code(code)
def _is_server_error_code(code):
return -32099 <= code <= -32000
def from_dict(error):
for exc_class in _EXCEPTIONS:
if exc_class.supports_code(error['code']):
return exc_class(**error)
return JsonRpcException(**error)
class JsonRpcInvalidRequest(JsonRpcException):
CODE = -32600
MESSAGE = 'Invalid Request'
class JsonRpcMethodNotFound(JsonRpcException):
CODE = -32601
MESSAGE = 'Method Not Found'
@classmethod
def of(cls, method):
return cls(message=cls.MESSAGE + ': ' + method)
class JsonRpcParseError(JsonRpcException):
CODE = -32700
MESSAGE = 'Parse Error'
class JsonRpcServerError(JsonRpcException):
def __init__(self, message, code, data=None):
assert _is_server_error_code(code)
super().__init__(
message=message, code=code, data=data)
@classmethod
def supports_code(cls, code):
return _is_server_error_code(code)