mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-23 19:20:47 +02:00
Improve comments
This commit is contained in:
parent
7045e5dcad
commit
5cdbafeeb6
2 changed files with 23 additions and 10 deletions
|
@ -58,20 +58,21 @@ class DomainHelper:
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_and_handle_errors(cls, domain, error_return_type, blank_ok=False):
|
def validate_and_handle_errors(cls, domain, error_return_type, blank_ok=False):
|
||||||
"""
|
"""
|
||||||
Validates the provided domain and handles any validation errors.
|
Validates a domain and returns an appropriate response based on the validation result.
|
||||||
|
|
||||||
This method attempts to validate the domain using the `validate` method. If validation fails,
|
|
||||||
it catches the exception and returns an appropriate error response. The type of the error response
|
|
||||||
(JSON response or form validation error) is determined by the `error_return_type` parameter.
|
|
||||||
|
|
||||||
|
This method uses the `validate` method to validate the domain. If validation fails, it catches the exception,
|
||||||
|
maps it to a corresponding error code, and returns a response based on the `error_return_type` parameter.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
domain (str): The domain to validate.
|
domain (str): The domain to validate.
|
||||||
error_return_type (ValidationErrorReturnType): The type of error response to return if validation fails.
|
error_return_type (ValidationErrorReturnType): Determines the type of response (JSON or form validation error).
|
||||||
blank_ok (bool, optional): Whether to return an exception if the input is blank. Defaults to False.
|
blank_ok (bool, optional): If True, blank input does not raise an exception. Defaults to False.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A tuple of the validated domain name, and the response
|
tuple: The validated domain (or None if validation failed), and the response (success or error).
|
||||||
""" # noqa
|
""" # noqa
|
||||||
|
|
||||||
|
# Map each exception to a corresponding error code
|
||||||
error_map = {
|
error_map = {
|
||||||
errors.BlankValueError: "required",
|
errors.BlankValueError: "required",
|
||||||
errors.ExtraDotsError: "extra_dots",
|
errors.ExtraDotsError: "extra_dots",
|
||||||
|
@ -79,21 +80,31 @@ class DomainHelper:
|
||||||
errors.RegistrySystemError: "error",
|
errors.RegistrySystemError: "error",
|
||||||
errors.InvalidDomainError: "invalid",
|
errors.InvalidDomainError: "invalid",
|
||||||
}
|
}
|
||||||
|
|
||||||
validated = None
|
validated = None
|
||||||
response = None
|
response = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Attempt to validate the domain
|
||||||
validated = cls.validate(domain, blank_ok)
|
validated = cls.validate(domain, blank_ok)
|
||||||
|
|
||||||
# Get a list of each possible exception, and the code to return
|
# Get a list of each possible exception, and the code to return
|
||||||
except tuple(error_map.keys()) as error:
|
except tuple(error_map.keys()) as error:
|
||||||
# For each exception, determine which code should be returned
|
# If an error is caught, get its type
|
||||||
|
error_type = type(error)
|
||||||
|
|
||||||
|
# Generate the response based on the error code and return type
|
||||||
response = DomainHelper._return_form_error_or_json_response(
|
response = DomainHelper._return_form_error_or_json_response(
|
||||||
error_return_type, code=error_map.get(type(error))
|
error_return_type, code=error_map.get(error_type)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
# For form validation, we do not need to display the success message
|
||||||
if error_return_type != ValidationErrorReturnType.FORM_VALIDATION_ERROR:
|
if error_return_type != ValidationErrorReturnType.FORM_VALIDATION_ERROR:
|
||||||
response = DomainHelper._return_form_error_or_json_response(
|
response = DomainHelper._return_form_error_or_json_response(
|
||||||
error_return_type, code="success", available=True
|
error_return_type, code="success", available=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Return the validated domain and the response (either error or success)
|
||||||
return (validated, response)
|
return (validated, response)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -18,6 +18,8 @@ class RegistrySystemError(ValueError):
|
||||||
|
|
||||||
|
|
||||||
class InvalidDomainError(ValueError):
|
class InvalidDomainError(ValueError):
|
||||||
|
"""Error class for situations where an invalid domain is supplied"""
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue