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 test_message(self) -> None: blog_post_model = blog_models.BlogPostModel( id='validblogid1', title='Sample Title', content='<p>hello</p>,', author_id='user', url_fragment='url_fragment_1') error = blog_validation_errors.DuplicateBlogUrlError(blog_post_model) self.assertEqual( error.stderr, 'DuplicateBlogUrlError in BlogPostModel(id="validblogid1"): url=%s' ' is not unique' % utils.quoted(blog_post_model.url_fragment))
def __init__(self, id_property, model_id, target_kind, target_id): """Initializes a new ModelRelationshipError. Args: id_property: ModelProperty. The property referring to the ID of the target model. model_id: bytes. The ID of the model with problematic ID property. target_kind: str. The kind of model the property refers to. target_id: bytes. The ID of the specific model that the property refers to. NOTE: This is the value of the ID property. """ # NOTE: IDs are converted to bytes because that's how they're read from # and written to the datastore. message = ( '%s=%s should correspond to the ID of an existing %s, but no such ' 'model exists' % ( id_property, utils.quoted(target_id), target_kind)) super(ModelRelationshipError, self).__init__( message, id_property.model_kind, model_id=model_id)
def __repr__(self): return '%s(stdout=%s, stderr=%s)' % ( self.__class__.__name__, utils.quoted(self.stdout), utils.quoted(self.stderr))
def test_quoted_string(self): # type: () -> None self.assertEqual(utils.quoted('a"b\'c'), '"a\\"b\'c"')
def __init__(self, model, regex_string): message = 'id does not match the expected regex=%s' % ( utils.quoted(regex_string)) super(ModelIdRegexError, self).__init__(message, model)
def __init__(self, model): message = 'url=%s is not unique' % utils.quoted(model.url_fragment) super(DuplicateBlogUrlError, self).__init__(message, model)
def __init__(self, model): message = 'title=%s is not unique' % utils.quoted(model.title) super(DuplicateBlogTitleError, self).__init__(message, model)
def __init__(self, model): # type: (Any) -> None # type: ignore[no-any-return] message = 'url=%s is not unique' % utils.quoted(model.url_fragment) super( # type: ignore[no-untyped-call] DuplicateBlogUrlError, self).__init__(message, model)
def __init__(self, model): # type: (Any) -> None # type: ignore[no-any-return] message = 'title=%s is not unique' % utils.quoted(model.title) super( # type: ignore[no-untyped-call] DuplicateBlogTitleError, self).__init__(message, model)