From 182d1a61c0347caba0ffc31f48c24721876f7c7d Mon Sep 17 00:00:00 2001 From: David Kennedy Date: Tue, 24 Oct 2023 13:02:02 -0400 Subject: [PATCH] more consolidation of error messages and their text --- src/epplibwrapper/__init__.py | 4 +-- src/epplibwrapper/errors.py | 3 -- src/registrar/forms/domain.py | 15 ++++++-- src/registrar/utility/errors.py | 37 +++++++++++++++++++ src/registrar/views/domain.py | 63 ++++++++++++++++++++++++++------- 5 files changed, 101 insertions(+), 21 deletions(-) diff --git a/src/epplibwrapper/__init__.py b/src/epplibwrapper/__init__.py index d0138d73c..dd6664a3a 100644 --- a/src/epplibwrapper/__init__.py +++ b/src/epplibwrapper/__init__.py @@ -45,7 +45,7 @@ except NameError: # Attn: these imports should NOT be at the top of the file try: 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.responses import extensions from epplib import responses @@ -61,6 +61,4 @@ __all__ = [ "info", "ErrorCode", "RegistryError", - "CANNOT_CONTACT_REGISTRY", - "GENERIC_ERROR", ] diff --git a/src/epplibwrapper/errors.py b/src/epplibwrapper/errors.py index dba5f328c..d34ed5e91 100644 --- a/src/epplibwrapper/errors.py +++ b/src/epplibwrapper/errors.py @@ -1,8 +1,5 @@ from enum import IntEnum -CANNOT_CONTACT_REGISTRY = "Update failed. Cannot contact the registry." -GENERIC_ERROR = "Value entered was wrong." - class ErrorCode(IntEnum): """ diff --git a/src/registrar/forms/domain.py b/src/registrar/forms/domain.py index 516e0abd2..93d42e53d 100644 --- a/src/registrar/forms/domain.py +++ b/src/registrar/forms/domain.py @@ -75,10 +75,21 @@ class DomainNameserverForm(forms.Form): if e.code == nsErrorCodes.GLUE_RECORD_NOT_ALLOWED: self.add_error( "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: - self.add_error("ip", NameserverError(code=nsErrorCodes.MISSING_IP)) + self.add_error( + "ip", + NameserverError( + code=nsErrorCodes.MISSING_IP, + nameserver=domain, + ip=ip_list, + ) + ) else: self.add_error("ip", str(e)) diff --git a/src/registrar/utility/errors.py b/src/registrar/utility/errors.py index c4bbc86c9..1f658b341 100644 --- a/src/registrar/utility/errors.py +++ b/src/registrar/utility/errors.py @@ -20,6 +20,41 @@ class ActionNotAllowed(Exception): 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): """Used in the NameserverError class for error mapping. @@ -29,6 +64,8 @@ class NameserverErrorCodes(IntEnum): 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 + - 5 UNABLE_TO_UPDATE_DOMAIN unable to update the domain + - 6 MISSING_HOST host is missing for a nameserver """ MISSING_IP = 1 diff --git a/src/registrar/views/domain.py b/src/registrar/views/domain.py index 01c31e9bc..b0f00f03a 100644 --- a/src/registrar/views/domain.py +++ b/src/registrar/views/domain.py @@ -24,7 +24,12 @@ from registrar.models import ( UserDomainRole, ) 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 ..forms import ( @@ -44,8 +49,6 @@ from epplibwrapper import ( common, extensions, RegistryError, - CANNOT_CONTACT_REGISTRY, - GENERIC_ERROR, ) from ..utility.email import send_templated_email, EmailSendingError @@ -311,18 +314,32 @@ class DomainNameserversView(DomainFormBaseView): try: self.object.nameservers = nameservers except NameserverError as Err: - # TODO: move into literal - messages.error(self.request, "Whoops, Nameservers Error") - # messages.error(self.request, GENERIC_ERROR) + # NamserverErrors *should* be caught in form; if reached here, + # there was an uncaught error in submission (through EPP) + messages.error( + self.request, + NameserverError( + code=nsErrorCodes.UNABLE_TO_UPDATE_DOMAIN + ) + ) logger.error(f"Nameservers error: {Err}") # TODO: registry is not throwing an error when no connection - # TODO: merge 1103 and use literals except RegistryError as Err: 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}") else: - messages.error(self.request, GENERIC_ERROR) + messages.error( + self.request, + GenericError( + code=GenericErrorCodes.GENERIC_ERROR + ) + ) logger.error(f"Registry error: {Err}") else: messages.success( @@ -688,7 +705,12 @@ class DomainSecurityEmailView(DomainFormBaseView): # If no default is created for security_contact, # then we cannot connect to the registry. 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()) contact.email = new_email @@ -697,13 +719,28 @@ class DomainSecurityEmailView(DomainFormBaseView): contact.save() except RegistryError as Err: 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}") else: - messages.error(self.request, GENERIC_ERROR) + messages.error( + self.request, + GenericError( + code=GenericErrorCodes.GENERIC_ERROR + ) + ) logger.error(f"Registry error: {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}") else: messages.success(