from django.utils.translation import gettext_lazy as _ from rest_framework import exceptions, status class ProjectBaseException(exceptions.APIException): """Base exception""" status_code = status.HTTP_400_BAD_REQUEST default_detail = _('Bad request') def __init__(self, data=None): if data: self.default_detail = { 'detail': self.default_detail, **data } super().__init__() class ServiceError(ProjectBaseException): """Service error.""" status_code = status.HTTP_503_SERVICE_UNAVAILABLE default_detail = _('Service is temporarily unavailable') class UserNotFoundError(ProjectBaseException): """The exception should be thrown when the user cannot get""" status_code = status.HTTP_401_UNAUTHORIZED default_detail = _('User not found') class PasswordRequestResetExists(ProjectBaseException): """The exception should be thrown when request for reset password is already exists and valid """ status_code = status.HTTP_400_BAD_REQUEST default_detail = _('Password request is already exists. Please wait.') class EmailSendingError(exceptions.APIException): """The exception should be thrown when unable to send an email""" status_code = status.HTTP_400_BAD_REQUEST default_detail = _('Unable to send message to mailbox %s') def __init__(self, recipient=None): if recipient: self.default_detail = { 'detail': self.default_detail % recipient, } super().__init__() class LocaleNotExisted(exceptions.APIException): """The exception should be thrown when passed locale isn't in model Language """ status_code = status.HTTP_400_BAD_REQUEST default_detail = _('Locale not found in database (%s)') def __init__(self, locale: str = None): if locale: self.default_detail = { 'detail': self.default_detail % locale } super().__init__() class NotValidUsernameError(exceptions.APIException): """The exception should be thrown when passed username has @ symbol """ status_code = status.HTTP_400_BAD_REQUEST default_detail = _('Wrong username') class NotValidTokenError(exceptions.APIException): """The exception should be thrown when token in url is not valid """ status_code = status.HTTP_400_BAD_REQUEST default_detail = _('Not valid token') class PasswordsAreEqual(exceptions.APIException): """The exception should be raised when passed password is the same as old ones """ status_code = status.HTTP_400_BAD_REQUEST default_detail = _('Password is already in use') class EmailConfirmedError(exceptions.APIException): """The exception should be raised when user email status is already confirmed """ status_code = status.HTTP_400_BAD_REQUEST default_detail = _('Email address is already confirmed') class WrongAuthCredentials(exceptions.APIException): """The exception should be raised when credentials is not valid for this user """ status_code = status.HTTP_401_UNAUTHORIZED default_detail = _('Wrong authorization credentials')