mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-05 10:43:23 +02:00
Return tuple and do error mapping
This commit is contained in:
parent
6b6aed8f24
commit
ce361cdd3b
3 changed files with 25 additions and 30 deletions
|
@ -91,10 +91,9 @@ def available(request, domain=""):
|
|||
Domain = apps.get_model("registrar.Domain")
|
||||
domain = request.GET.get("domain", "")
|
||||
|
||||
json_response = Domain.validate_and_handle_errors(
|
||||
_, json_response = Domain.validate_and_handle_errors(
|
||||
domain=domain,
|
||||
error_return_type=ValidationErrorReturnType.JSON_RESPONSE,
|
||||
display_success=True,
|
||||
error_return_type=ValidationErrorReturnType.JSON_RESPONSE,
|
||||
)
|
||||
return json_response
|
||||
|
||||
|
|
|
@ -383,7 +383,7 @@ class AlternativeDomainForm(RegistrarForm):
|
|||
def clean_alternative_domain(self):
|
||||
"""Validation code for domain names."""
|
||||
requested = self.cleaned_data.get("alternative_domain", None)
|
||||
validated = DraftDomain.validate_and_handle_errors(
|
||||
validated, _ = DraftDomain.validate_and_handle_errors(
|
||||
requested, ValidationErrorReturnType.FORM_VALIDATION_ERROR, blank_ok=True
|
||||
)
|
||||
return validated
|
||||
|
@ -461,7 +461,7 @@ class DotGovDomainForm(RegistrarForm):
|
|||
def clean_requested_domain(self):
|
||||
"""Validation code for domain names."""
|
||||
requested = self.cleaned_data.get("requested_domain", None)
|
||||
validated = DraftDomain.validate_and_handle_errors(requested, ValidationErrorReturnType.FORM_VALIDATION_ERROR)
|
||||
validated, _ = DraftDomain.validate_and_handle_errors(requested, ValidationErrorReturnType.FORM_VALIDATION_ERROR)
|
||||
return validated
|
||||
|
||||
requested_domain = forms.CharField(label="What .gov domain do you want?")
|
||||
|
|
|
@ -57,7 +57,7 @@ class DomainHelper:
|
|||
return domain
|
||||
|
||||
@classmethod
|
||||
def validate_and_handle_errors(cls, domain, error_return_type, blank_ok=False, display_success=False):
|
||||
def validate_and_handle_errors(cls, domain, error_return_type, blank_ok=False):
|
||||
"""
|
||||
Validates the provided domain and handles any validation errors.
|
||||
|
||||
|
@ -65,40 +65,36 @@ class DomainHelper:
|
|||
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.
|
||||
|
||||
If validation is successful and `display_success` is True, it returns a success response.
|
||||
Otherwise, it returns the validated domain.
|
||||
|
||||
Args:
|
||||
domain (str): The domain to validate.
|
||||
error_return_type (ValidationErrorReturnType): The type of error response to return if validation fails.
|
||||
blank_ok (bool, optional): Whether to return an exception if the input is blank. Defaults to False.
|
||||
display_success (bool, optional): Whether to return a success response if validation is successful. Defaults to False.
|
||||
|
||||
Returns:
|
||||
The error response if validation fails,
|
||||
the success response if validation is successful and `display_success` is True,
|
||||
or the validated domain otherwise.
|
||||
A tuple of the validated domain name, and the response
|
||||
""" # noqa
|
||||
|
||||
error_map = {
|
||||
errors.BlankValueError: "required",
|
||||
errors.ExtraDotsError: "extra_dots",
|
||||
errors.DomainUnavailableError: "unavailable",
|
||||
errors.RegistrySystemError: "error",
|
||||
errors.InvalidDomainError: "invalid",
|
||||
}
|
||||
validated = None
|
||||
response = None
|
||||
try:
|
||||
validated = cls.validate(domain, blank_ok)
|
||||
except errors.BlankValueError:
|
||||
return DomainHelper._return_form_error_or_json_response(error_return_type, code="required")
|
||||
except errors.ExtraDotsError:
|
||||
return DomainHelper._return_form_error_or_json_response(error_return_type, code="extra_dots")
|
||||
except errors.DomainUnavailableError:
|
||||
return DomainHelper._return_form_error_or_json_response(error_return_type, code="unavailable")
|
||||
except errors.RegistrySystemError:
|
||||
return DomainHelper._return_form_error_or_json_response(error_return_type, code="error")
|
||||
except errors.InvalidDomainError:
|
||||
return DomainHelper._return_form_error_or_json_response(error_return_type, code="invalid")
|
||||
# Get a list of each possible exception, and the code to return
|
||||
except tuple(error_map.keys()) as error:
|
||||
# For each exception, determine which code should be returned
|
||||
response = DomainHelper._return_form_error_or_json_response(
|
||||
error_return_type, code=error_map.get(type(error))
|
||||
)
|
||||
else:
|
||||
if display_success:
|
||||
return DomainHelper._return_form_error_or_json_response(
|
||||
error_return_type, code="success", available=True
|
||||
)
|
||||
else:
|
||||
return validated
|
||||
response = DomainHelper._return_form_error_or_json_response(
|
||||
error_return_type, code="success", available=True
|
||||
)
|
||||
return (validated, response)
|
||||
|
||||
@staticmethod
|
||||
def _return_form_error_or_json_response(return_type: ValidationErrorReturnType, code, available=False):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue