more consolidation of error messages and their text

This commit is contained in:
David Kennedy 2023-10-24 13:02:02 -04:00
parent d532449b3d
commit 182d1a61c0
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
5 changed files with 101 additions and 21 deletions

View file

@ -45,7 +45,7 @@ except NameError:
# Attn: these imports should NOT be at the top of the file # Attn: these imports should NOT be at the top of the file
try: try:
from .client import CLIENT, commands from .client import CLIENT, commands
from .errors import RegistryError, ErrorCode, CANNOT_CONTACT_REGISTRY, GENERIC_ERROR from .errors import RegistryError, ErrorCode
from epplib.models import common, info from epplib.models import common, info
from epplib.responses import extensions from epplib.responses import extensions
from epplib import responses from epplib import responses
@ -61,6 +61,4 @@ __all__ = [
"info", "info",
"ErrorCode", "ErrorCode",
"RegistryError", "RegistryError",
"CANNOT_CONTACT_REGISTRY",
"GENERIC_ERROR",
] ]

View file

@ -1,8 +1,5 @@
from enum import IntEnum from enum import IntEnum
CANNOT_CONTACT_REGISTRY = "Update failed. Cannot contact the registry."
GENERIC_ERROR = "Value entered was wrong."
class ErrorCode(IntEnum): class ErrorCode(IntEnum):
""" """

View file

@ -75,10 +75,21 @@ class DomainNameserverForm(forms.Form):
if e.code == nsErrorCodes.GLUE_RECORD_NOT_ALLOWED: if e.code == nsErrorCodes.GLUE_RECORD_NOT_ALLOWED:
self.add_error( self.add_error(
"server", "server",
NameserverError(code=nsErrorCodes.GLUE_RECORD_NOT_ALLOWED), NameserverError(
code=nsErrorCodes.GLUE_RECORD_NOT_ALLOWED,
nameserver=domain,
ip=ip_list
),
) )
elif e.code == nsErrorCodes.MISSING_IP: elif e.code == nsErrorCodes.MISSING_IP:
self.add_error("ip", NameserverError(code=nsErrorCodes.MISSING_IP)) self.add_error(
"ip",
NameserverError(
code=nsErrorCodes.MISSING_IP,
nameserver=domain,
ip=ip_list,
)
)
else: else:
self.add_error("ip", str(e)) self.add_error("ip", str(e))

View file

@ -20,6 +20,41 @@ class ActionNotAllowed(Exception):
pass pass
class GenericErrorCodes(IntEnum):
"""Used across the registrar for
error mapping.
Overview of generic error codes:
- 1 GENERIC_ERROR a generic value error
- 2 CANNOT_CONTACT_REGISTRY a connection error w registry
"""
GENERIC_ERROR = 1
CANNOT_CONTACT_REGISTRY = 2
class GenericError(Exception):
"""
GenericError class used to raise exceptions across
the registrar
"""
_error_mapping = {
GenericErrorCodes.CANNOT_CONTACT_REGISTRY: "Update failed. Cannot contact the registry.",
GenericErrorCodes.GENERIC_ERROR: (
"Value entered was wrong."
),
}
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 NameserverErrorCodes(IntEnum): class NameserverErrorCodes(IntEnum):
"""Used in the NameserverError class for """Used in the NameserverError class for
error mapping. error mapping.
@ -29,6 +64,8 @@ class NameserverErrorCodes(IntEnum):
value but is not a subdomain value but is not a subdomain
- 3 INVALID_IP invalid ip address format or invalid version - 3 INVALID_IP invalid ip address format or invalid version
- 4 TOO_MANY_HOSTS more than the max allowed host values - 4 TOO_MANY_HOSTS more than the max allowed host values
- 5 UNABLE_TO_UPDATE_DOMAIN unable to update the domain
- 6 MISSING_HOST host is missing for a nameserver
""" """
MISSING_IP = 1 MISSING_IP = 1

View file

@ -24,7 +24,12 @@ from registrar.models import (
UserDomainRole, UserDomainRole,
) )
from registrar.models.public_contact import PublicContact from registrar.models.public_contact import PublicContact
from registrar.utility.errors import NameserverError from registrar.utility.errors import (
GenericError,
GenericErrorCodes,
NameserverError,
NameserverErrorCodes as nsErrorCodes,
)
from registrar.models.utility.contact_error import ContactError from registrar.models.utility.contact_error import ContactError
from ..forms import ( from ..forms import (
@ -44,8 +49,6 @@ from epplibwrapper import (
common, common,
extensions, extensions,
RegistryError, RegistryError,
CANNOT_CONTACT_REGISTRY,
GENERIC_ERROR,
) )
from ..utility.email import send_templated_email, EmailSendingError from ..utility.email import send_templated_email, EmailSendingError
@ -311,18 +314,32 @@ class DomainNameserversView(DomainFormBaseView):
try: try:
self.object.nameservers = nameservers self.object.nameservers = nameservers
except NameserverError as Err: except NameserverError as Err:
# TODO: move into literal # NamserverErrors *should* be caught in form; if reached here,
messages.error(self.request, "Whoops, Nameservers Error") # there was an uncaught error in submission (through EPP)
# messages.error(self.request, GENERIC_ERROR) messages.error(
self.request,
NameserverError(
code=nsErrorCodes.UNABLE_TO_UPDATE_DOMAIN
)
)
logger.error(f"Nameservers error: {Err}") logger.error(f"Nameservers error: {Err}")
# TODO: registry is not throwing an error when no connection # TODO: registry is not throwing an error when no connection
# TODO: merge 1103 and use literals
except RegistryError as Err: except RegistryError as Err:
if Err.is_connection_error(): if Err.is_connection_error():
messages.error(self.request, CANNOT_CONTACT_REGISTRY) messages.error(
self.request,
GenericError(
code=GenericErrorCodes.CANNOT_CONTACT_REGISTRY
)
)
logger.error(f"Registry connection error: {Err}") logger.error(f"Registry connection error: {Err}")
else: else:
messages.error(self.request, GENERIC_ERROR) messages.error(
self.request,
GenericError(
code=GenericErrorCodes.GENERIC_ERROR
)
)
logger.error(f"Registry error: {Err}") logger.error(f"Registry error: {Err}")
else: else:
messages.success( messages.success(
@ -688,7 +705,12 @@ class DomainSecurityEmailView(DomainFormBaseView):
# If no default is created for security_contact, # If no default is created for security_contact,
# then we cannot connect to the registry. # then we cannot connect to the registry.
if contact is None: if contact is None:
messages.error(self.request, CANNOT_CONTACT_REGISTRY) messages.error(
self.request,
GenericError(
code=GenericErrorCodes.CANNOT_CONTACT_REGISTRY
)
)
return redirect(self.get_success_url()) return redirect(self.get_success_url())
contact.email = new_email contact.email = new_email
@ -697,13 +719,28 @@ class DomainSecurityEmailView(DomainFormBaseView):
contact.save() contact.save()
except RegistryError as Err: except RegistryError as Err:
if Err.is_connection_error(): if Err.is_connection_error():
messages.error(self.request, CANNOT_CONTACT_REGISTRY) messages.error(
self.request,
GenericError(
code=GenericErrorCodes.CANNOT_CONTACT_REGISTRY
)
)
logger.error(f"Registry connection error: {Err}") logger.error(f"Registry connection error: {Err}")
else: else:
messages.error(self.request, GENERIC_ERROR) messages.error(
self.request,
GenericError(
code=GenericErrorCodes.GENERIC_ERROR
)
)
logger.error(f"Registry error: {Err}") logger.error(f"Registry error: {Err}")
except ContactError as Err: except ContactError as Err:
messages.error(self.request, GENERIC_ERROR) messages.error(
self.request,
GenericError(
code=GenericErrorCodes.GENERIC_ERROR
)
)
logger.error(f"Generic registry error: {Err}") logger.error(f"Generic registry error: {Err}")
else: else:
messages.success( messages.success(