def convert_to_dbexception(self, args): """Converts from a VitessError to the appropriate dbexceptions class. Args: args: argument tuple to use to create the new exception. Returns: An exception from dbexceptions. """ # FIXME(alainjobart): this is extremely confusing: self.message is only # used for integrity errors, and nothing else. The other cases # have to provide the message in the args. if self.code == vtrpc_pb2.UNAVAILABLE: if throttler_err_re.search(self.message): return dbexceptions.ThrottledError(args) return dbexceptions.TransientError(args) if self.code == vtrpc_pb2.FAILED_PRECONDITION: return dbexceptions.QueryNotServed(args) if self.code == vtrpc_pb2.ALREADY_EXISTS: # Prune the error message to truncate after the mysql errno, since # the error message may contain the query string with bind variables. msg = self.message.lower() parts = self._errno_pattern.split(msg) pruned_msg = msg[:msg.find(parts[2])] new_args = (pruned_msg, ) + tuple(args[1:]) return dbexceptions.IntegrityError(new_args) if self.code == vtrpc_pb2.INVALID_ARGUMENT: return dbexceptions.ProgrammingError(args) return dbexceptions.DatabaseError(args)
def _convert_exception(exc, *args, **kwargs): """This parses the protocol exceptions to the api interface exceptions. This also logs the exception and increments the appropriate error counters. Args: exc: raw protocol exception. *args: additional args from the raising site. **kwargs: additional keyword args from the raising site. They will be converted into a single string, and added as an extra arg to the exception. Returns: Api interface exceptions - dbexceptions with new args. """ kwargs_as_str = vtgate_utils.convert_exception_kwargs(kwargs) exc.args += args if kwargs_as_str: exc.args += kwargs_as_str, new_args = (type(exc).__name__, ) + exc.args if isinstance(exc, vtgate_utils.VitessError): new_exc = exc.convert_to_dbexception(new_args) elif isinstance(exc, grpc.RpcError): # Most RpcErrors should also implement Call so we can get details. if isinstance(exc, grpc.Call): code = exc.code() details = exc.details() if code == grpc.StatusCode.DEADLINE_EXCEEDED: new_exc = dbexceptions.TimeoutError(new_args) elif code == grpc.StatusCode.UNAVAILABLE: if vtgate_utils.throttler_err_re.search(details): return dbexceptions.ThrottledError(new_args) else: return dbexceptions.TransientError(new_args) elif code == grpc.StatusCode.ALREADY_EXISTS: new_exc = _prune_integrity_error(details, new_args) elif code == grpc.StatusCode.FAILED_PRECONDITION: return dbexceptions.QueryNotServed(details, new_args) elif code == grpc.StatusCode.INVALID_ARGUMENT: return dbexceptions.ProgrammingError(details, new_args) else: # Other RPC error that we don't specifically handle. new_exc = dbexceptions.DatabaseError(new_args + (code, details)) else: # RPC error that doesn't provide code and details. # Don't let gRPC-specific errors leak beyond this package. new_exc = dbexceptions.DatabaseError(new_args + (exc, )) else: new_exc = exc vtgate_utils.log_exception(new_exc, keyspace=kwargs.get('keyspace'), tablet_type=kwargs.get('tablet_type')) return new_exc
def _convert_exception(exc, *args, **kwargs): """This parses the protocol exceptions to the api interface exceptions. This also logs the exception and increments the appropriate error counters. Args: exc: raw protocol exception. *args: additional args from the raising site. **kwargs: additional keyword args from the raising site. They will be converted into a single string, and added as an extra arg to the exception. Returns: Api interface exceptions - dbexceptions with new args. """ kwargs_as_str = vtgate_utils.convert_exception_kwargs(kwargs) exc.args += args if kwargs_as_str: exc.args += kwargs_as_str, new_args = (type(exc).__name__,) + exc.args if isinstance(exc, vtgate_utils.VitessError): new_exc = exc.convert_to_dbexception(new_args) elif isinstance(exc, face.ExpirationError): # face.ExpirationError is returned by the gRPC library when # a request times out. Note it is a subclass of face.AbortionError # so we have to test for it before. new_exc = dbexceptions.TimeoutError(new_args) elif isinstance(exc, face.AbortionError): # face.AbortionError is the toplevel error returned by gRPC for any # RPC that finishes earlier than expected. msg = exc.details if exc.code == interfaces.StatusCode.UNAVAILABLE: if _throttler_err_pattern.search(msg): return dbexceptions.ThrottledError(new_args) else: return dbexceptions.TransientError(new_args) elif exc.code == interfaces.StatusCode.ALREADY_EXISTS: new_exc = _prune_integrity_error(msg, new_args) elif exc.code == interfaces.StatusCode.FAILED_PRECONDITION: return dbexceptions.QueryNotServed(msg, new_args) else: # Unhandled RPC application error new_exc = dbexceptions.DatabaseError(new_args + (msg,)) else: new_exc = exc vtgate_utils.log_exception( new_exc, keyspace=kwargs.get('keyspace'), tablet_type=kwargs.get('tablet_type')) return new_exc