error handling for generic error messages in nameserver, dsdata and security email forms

This commit is contained in:
David Kennedy 2023-11-14 15:00:03 -05:00
parent 778fbdb737
commit 624871de2d
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
2 changed files with 75 additions and 4 deletions

View file

@ -69,6 +69,7 @@ class NameserverErrorCodes(IntEnum):
- 5 UNABLE_TO_UPDATE_DOMAIN unable to update the domain
- 6 MISSING_HOST host is missing for a nameserver
- 7 INVALID_HOST host is invalid for a nameserver
- 8 BAD_DATA bad data input for nameserver
"""
MISSING_IP = 1
@ -78,6 +79,7 @@ class NameserverErrorCodes(IntEnum):
UNABLE_TO_UPDATE_DOMAIN = 5
MISSING_HOST = 6
INVALID_HOST = 7
BAD_DATA = 8
class NameserverError(Exception):
@ -96,6 +98,9 @@ class NameserverError(Exception):
),
NameserverErrorCodes.MISSING_HOST: ("Name server must be provided to enter IP address."),
NameserverErrorCodes.INVALID_HOST: ("Enter a name server in the required format, like ns1.example.com"),
NameserverErrorCodes.BAD_DATA: (
"Theres something wrong with the name server information you provided. If you need help email us at help@get.gov."
),
}
def __init__(self, *args, code=None, nameserver=None, ip=None, **kwargs):
@ -112,3 +117,65 @@ class NameserverError(Exception):
def __str__(self):
return f"{self.message}"
class DsDataErrorCodes(IntEnum):
"""Used in the DsDataError class for
error mapping.
Overview of ds data error codes:
- 1 BAD_DATA bad data input in ds data
"""
BAD_DATA = 1
class DsDataError(Exception):
"""
DsDataError class used to raise exceptions on
the ds data getter
"""
_error_mapping = {
DsDataErrorCodes.BAD_DATA: (
"Theres something wrong with the DS data you provided. If you need help email us at help@get.gov."
)
}
def __init__(self, *args, code=None, **kwargs):
super().__init__(*args, **kwargs)
self.code = code
if self.code in self._error_mapping:
self.message = self._error_mapping.get(self.code)
def __str__(self):
return f"{self.message}"
class SecurityEmailErrorCodes(IntEnum):
"""Used in the SecurityEmailError class for
error mapping.
Overview of security email error codes:
- 1 BAD_DATA bad data input in security email
"""
BAD_DATA = 1
class SecurityEmailError(Exception):
"""
SecurityEmailError class used to raise exceptions on
the security email form
"""
_error_mapping = {
SecurityEmailErrorCodes.BAD_DATA: ("Enter an email address in the required format, like name@example.com.")
}
def __init__(self, *args, code=None, **kwargs):
super().__init__(*args, **kwargs)
self.code = code
if self.code in self._error_mapping:
self.message = self._error_mapping.get(self.code)
def __str__(self):
return f"{self.message}"

View file

@ -28,6 +28,10 @@ from registrar.utility.errors import (
GenericErrorCodes,
NameserverError,
NameserverErrorCodes as nsErrorCodes,
DsDataError,
DsDataErrorCodes,
SecurityEmailError,
SecurityEmailErrorCodes,
)
from registrar.models.utility.contact_error import ContactError
@ -315,7 +319,7 @@ class DomainNameserversView(DomainFormBaseView):
)
logger.error(f"Registry connection error: {Err}")
else:
messages.error(self.request, GenericError(code=GenericErrorCodes.GENERIC_ERROR))
messages.error(self.request, NameserverError(code=nsErrorCodes.BAD_DATA))
logger.error(f"Registry error: {Err}")
else:
messages.success(
@ -491,7 +495,7 @@ class DomainDsDataView(DomainFormBaseView):
)
logger.error(f"Registry connection error: {err}")
else:
messages.error(self.request, GenericError(code=GenericErrorCodes.GENERIC_ERROR))
messages.error(self.request, DsDataError(code=DsDataErrorCodes.BAD_DATA))
logger.error(f"Registry error: {err}")
return self.form_invalid(formset)
else:
@ -581,10 +585,10 @@ class DomainSecurityEmailView(DomainFormBaseView):
)
logger.error(f"Registry connection error: {Err}")
else:
messages.error(self.request, GenericError(code=GenericErrorCodes.GENERIC_ERROR))
messages.error(self.request, SecurityEmailError(code=SecurityEmailErrorCodes.BAD_DATA))
logger.error(f"Registry error: {Err}")
except ContactError as Err:
messages.error(self.request, GenericError(code=GenericErrorCodes.GENERIC_ERROR))
messages.error(self.request, SecurityEmailError(code=SecurityEmailErrorCodes.BAD_DATA))
logger.error(f"Generic registry error: {Err}")
else:
messages.success(self.request, "The security email for this domain has been updated.")