diff --git a/src/registrar/models/domain.py b/src/registrar/models/domain.py index be7d8ee59..025a7392f 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -15,7 +15,7 @@ from epplibwrapper import ( RegistryError, ErrorCode, ) -from registrar.models.utility.nameserver_error import ( +from registrar.utility.errors import ( NameserverError, NameserverErrorCodes as nsErrorCodes, ) diff --git a/src/registrar/models/utility/nameserver_error.py b/src/registrar/models/utility/nameserver_error.py deleted file mode 100644 index 96d4b5af3..000000000 --- a/src/registrar/models/utility/nameserver_error.py +++ /dev/null @@ -1,51 +0,0 @@ -from enum import IntEnum - - -class NameserverErrorCodes(IntEnum): - """Used in the NameserverError class for - error mapping. - Overview of nameserver error codes: - - 1 MISSING_IP ip address is missing for a nameserver - - 2 GLUE_RECORD_NOT_ALLOWED a host has a nameserver - value but is not a subdomain - - 3 INVALID_IP invalid ip address format or invalid version - - 4 TOO_MANY_HOSTS more than the max allowed host values - """ - - MISSING_IP = 1 - GLUE_RECORD_NOT_ALLOWED = 2 - INVALID_IP = 3 - TOO_MANY_HOSTS = 4 - - -class NameserverError(Exception): - """ - NameserverError class used to raise exceptions on - the nameserver getter - """ - - _error_mapping = { - NameserverErrorCodes.MISSING_IP: "Nameserver {} needs to have an " - "IP address because it is a subdomain", - NameserverErrorCodes.GLUE_RECORD_NOT_ALLOWED: "Nameserver {} cannot be linked " - "because it is not a subdomain", - NameserverErrorCodes.INVALID_IP: "Nameserver {} has an invalid IP address: {}", - NameserverErrorCodes.TOO_MANY_HOSTS: ( - "Too many hosts provided, you may not have more than " "13 nameservers." - ), - } - - def __init__(self, *args, code=None, nameserver=None, ip=None, **kwargs): - super().__init__(*args, **kwargs) - self.code = code - if self.code in self._error_mapping: - self.message = self._error_mapping.get(self.code) - if nameserver is not None and ip is not None: - self.message = self.message.format(str(nameserver), str(ip)) - elif nameserver is not None: - self.message = self.message.format(str(nameserver)) - elif ip is not None: - self.message = self.message.format(str(ip)) - - def __str__(self): - return f"{self.message}" diff --git a/src/registrar/tests/test_models_domain.py b/src/registrar/tests/test_models_domain.py index 1f4aaed5a..9864c7f1a 100644 --- a/src/registrar/tests/test_models_domain.py +++ b/src/registrar/tests/test_models_domain.py @@ -16,7 +16,7 @@ from registrar.models.domain_information import DomainInformation from registrar.models.draft_domain import DraftDomain from registrar.models.public_contact import PublicContact from registrar.models.user import User -from registrar.models.utility.nameserver_error import NameserverError +from registrar.utility.errors import NameserverError from .common import MockEppLib from django_fsm import TransitionNotAllowed # type: ignore from epplibwrapper import ( diff --git a/src/registrar/tests/test_nameserver_error.py b/src/registrar/tests/test_nameserver_error.py index 2f11977ac..c64717eb5 100644 --- a/src/registrar/tests/test_nameserver_error.py +++ b/src/registrar/tests/test_nameserver_error.py @@ -1,6 +1,6 @@ from django.test import TestCase -from registrar.models.utility.nameserver_error import ( +from registrar.utility.errors import ( NameserverError, NameserverErrorCodes as nsErrorCodes, ) diff --git a/src/registrar/utility/errors.py b/src/registrar/utility/errors.py index 3b17a17c7..98c2e6667 100644 --- a/src/registrar/utility/errors.py +++ b/src/registrar/utility/errors.py @@ -1,3 +1,6 @@ +from enum import IntEnum + + class BlankValueError(ValueError): pass @@ -8,3 +11,53 @@ class ExtraDotsError(ValueError): class DomainUnavailableError(ValueError): pass + + +class NameserverErrorCodes(IntEnum): + """Used in the NameserverError class for + error mapping. + Overview of nameserver error codes: + - 1 MISSING_IP ip address is missing for a nameserver + - 2 GLUE_RECORD_NOT_ALLOWED a host has a nameserver + value but is not a subdomain + - 3 INVALID_IP invalid ip address format or invalid version + - 4 TOO_MANY_HOSTS more than the max allowed host values + """ + + MISSING_IP = 1 + GLUE_RECORD_NOT_ALLOWED = 2 + INVALID_IP = 3 + TOO_MANY_HOSTS = 4 + + +class NameserverError(Exception): + """ + NameserverError class used to raise exceptions on + the nameserver getter + """ + + _error_mapping = { + NameserverErrorCodes.MISSING_IP: "Nameserver {} needs to have an " + "IP address because it is a subdomain", + NameserverErrorCodes.GLUE_RECORD_NOT_ALLOWED: "Nameserver {} cannot be linked " + "because it is not a subdomain", + NameserverErrorCodes.INVALID_IP: "Nameserver {} has an invalid IP address: {}", + NameserverErrorCodes.TOO_MANY_HOSTS: ( + "Too many hosts provided, you may not have more than " "13 nameservers." + ), + } + + def __init__(self, *args, code=None, nameserver=None, ip=None, **kwargs): + super().__init__(*args, **kwargs) + self.code = code + if self.code in self._error_mapping: + self.message = self._error_mapping.get(self.code) + if nameserver is not None and ip is not None: + self.message = self.message.format(str(nameserver), str(ip)) + elif nameserver is not None: + self.message = self.message.format(str(nameserver)) + elif ip is not None: + self.message = self.message.format(str(ip)) + + def __str__(self): + return f"{self.message}"