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 isinstance(message, str):
            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)
示例#4
0
 def __repr__(self) -> str:
     return '%s(stdout=%s, stderr=%s)' % (self.__class__.__name__,
                                          utils.quoted(self.stdout),
                                          utils.quoted(self.stderr))
示例#5
0
 def test_quoted_string(self) -> None:
     self.assertEqual(utils.quoted('a"b\'c'), '"a\\"b\'c"')
示例#6
0
 def __init__(self, model):
     message = 'url=%s is not unique' % utils.quoted(model.url_fragment)
     super(DuplicateBlogUrlError, self).__init__(message, model)
示例#7
0
 def __init__(self, model):
     message = 'title=%s is not unique' % utils.quoted(model.title)
     super(DuplicateBlogTitleError, self).__init__(message, model)
示例#8
0
 def __init__(self, model: base_models.BaseModel,
              regex_string: str) -> None:
     message = 'id does not match the expected regex=%s' % (
         utils.quoted(regex_string))
     super(ModelIdRegexError, self).__init__(message, model)
示例#9
0
 def __init__(
     self, model: Union[blog_models.BlogPostModel,
                        blog_models.BlogPostSummaryModel]
 ) -> None:
     message = 'url=%s is not unique' % utils.quoted(model.url_fragment)
     super(DuplicateBlogUrlError, self).__init__(message, model)
示例#10
0
 def __init__(
     self, model: Union[blog_models.BlogPostModel,
                        blog_models.BlogPostSummaryModel]
 ) -> None:
     message = 'title=%s is not unique' % utils.quoted(model.title)
     super(DuplicateBlogTitleError, self).__init__(message, model)