def __init__(self, message, model_or_kind, model_id=None): """Initializes a new audit error. Args: message: str. The message describing the error. model_or_kind: Model|bytes. If model_id is not provided, then this is a model (type: BaseModel). Otherwise, this is a model's kind (type: bytes). model_id: bytes|None. The model's ID, or None when model_or_kind is a model. Raises: TypeError. When the input message is not a string. ValueError. When the input message is empty. """ if not python_utils.is_string(message): raise TypeError('message must be a string') if not message: raise ValueError('message must be a non-empty string') if model_id is None: model_id = job_utils.get_model_id(model_or_kind) model_kind = job_utils.get_model_kind(model_or_kind) else: model_kind = model_or_kind error_message = '%s in %s(id=%s): %s' % ( self.__class__.__name__, model_kind, utils.quoted(model_id), message) super(BaseAuditError, self).__init__(stderr=error_message)
def message(self, message): """Sets the error message. Args: message: str. The error message. Raises: TypeError. When self.message has already been assigned a value. TypeError. When the input message is not a string. ValueError. When the input message is empty. """ if python_utils.is_string(self._message): raise TypeError('self.message must be assigned to exactly once') if not python_utils.is_string(message): raise TypeError('self.message must be a string') if not message: raise ValueError('self.message must be a non-empty string') model_kind, model_id = self._message self._message = '%s in %s(id=%r): %s' % (self.__class__.__name__, model_kind, model_id, message)
def message(self): """Returns the error message, which includes the erroneous model's id. Returns: str. The error message. Raises: NotImplementedError. When self.message was never assigned a value. """ if not python_utils.is_string(self._message): raise NotImplementedError( 'self.message must be assigned a value in __init__') return self._message
def test_is_string(self): self.assertTrue(python_utils.is_string('abc')) self.assertFalse(python_utils.is_string(123)) self.assertFalse(python_utils.is_string(['a', 'b', 'c']))