Source code for mimesis.exceptions

# -*- coding: utf-8 -*-

"""Custom exceptions which used in Mimesis."""

from typing import Any, Optional


[docs]class LocaleError(ValueError): """Raised when a locale isn't supported.""" def __init__(self, locale: Optional[str] = None) -> None: """Initialize attributes for informative output. :param locale: Locale. """ self.locale = locale self.message = "Locale «{}» is not supported" def __str__(self) -> str: return self.message.format(self.locale)
[docs]class SchemaError(ValueError): """Raised when schema is unsupported.""" def __str__(self) -> str: return "Schema should a callable object."
[docs]class NonEnumerableError(TypeError): """Raised when object is not instance of Enum.""" message = "You should use one item of: «{}» of the object mimesis.enums.{}" def __init__(self, enum_obj: Any) -> None: """Initialize attributes for informative output. :param enum_obj: Enum object. """ if enum_obj: self.name = enum_obj self.items = ", ".join([str(i) for i in enum_obj]) else: self.items = "" def __str__(self) -> str: return self.message.format(self.items, self.name.__name__)
[docs]class FieldError(ValueError): def __init__(self, name: Optional[str] = None) -> None: """Initialize attributes for more informative output. :param name: Name of the field.. """ self.name = name self.message = "A field «{}» is not supported." self.message_none = "Field cannot be None." def __str__(self) -> str: if self.name is None: return self.message_none return self.message.format(self.name)