mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-04 10:13:30 +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 = apps.get_model("registrar.Domain")
|
||||||
domain = request.GET.get("domain", "")
|
domain = request.GET.get("domain", "")
|
||||||
|
|
||||||
json_response = Domain.validate_and_handle_errors(
|
_, json_response = Domain.validate_and_handle_errors(
|
||||||
domain=domain,
|
domain=domain,
|
||||||
error_return_type=ValidationErrorReturnType.JSON_RESPONSE,
|
error_return_type=ValidationErrorReturnType.JSON_RESPONSE,
|
||||||
display_success=True,
|
|
||||||
)
|
)
|
||||||
return json_response
|
return json_response
|
||||||
|
|
||||||
|
|
|
@ -383,7 +383,7 @@ class AlternativeDomainForm(RegistrarForm):
|
||||||
def clean_alternative_domain(self):
|
def clean_alternative_domain(self):
|
||||||
"""Validation code for domain names."""
|
"""Validation code for domain names."""
|
||||||
requested = self.cleaned_data.get("alternative_domain", None)
|
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
|
requested, ValidationErrorReturnType.FORM_VALIDATION_ERROR, blank_ok=True
|
||||||
)
|
)
|
||||||
return validated
|
return validated
|
||||||
|
@ -461,7 +461,7 @@ class DotGovDomainForm(RegistrarForm):
|
||||||
def clean_requested_domain(self):
|
def clean_requested_domain(self):
|
||||||
"""Validation code for domain names."""
|
"""Validation code for domain names."""
|
||||||
requested = self.cleaned_data.get("requested_domain", None)
|
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
|
return validated
|
||||||
|
|
||||||
requested_domain = forms.CharField(label="What .gov domain do you want?")
|
requested_domain = forms.CharField(label="What .gov domain do you want?")
|
||||||
|
|
|
@ -57,7 +57,7 @@ class DomainHelper:
|
||||||
return domain
|
return domain
|
||||||
|
|
||||||
@classmethod
|
@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.
|
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
|
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.
|
(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:
|
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): 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.
|
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:
|
Returns:
|
||||||
The error response if validation fails,
|
A tuple of the validated domain name, and the response
|
||||||
the success response if validation is successful and `display_success` is True,
|
|
||||||
or the validated domain otherwise.
|
|
||||||
""" # noqa
|
""" # noqa
|
||||||
|
error_map = {
|
||||||
|
errors.BlankValueError: "required",
|
||||||
|
errors.ExtraDotsError: "extra_dots",
|
||||||
|
errors.DomainUnavailableError: "unavailable",
|
||||||
|
errors.RegistrySystemError: "error",
|
||||||
|
errors.InvalidDomainError: "invalid",
|
||||||
|
}
|
||||||
|
validated = None
|
||||||
|
response = None
|
||||||
try:
|
try:
|
||||||
validated = cls.validate(domain, blank_ok)
|
validated = cls.validate(domain, blank_ok)
|
||||||
except errors.BlankValueError:
|
# Get a list of each possible exception, and the code to return
|
||||||
return DomainHelper._return_form_error_or_json_response(error_return_type, code="required")
|
except tuple(error_map.keys()) as error:
|
||||||
except errors.ExtraDotsError:
|
# For each exception, determine which code should be returned
|
||||||
return DomainHelper._return_form_error_or_json_response(error_return_type, code="extra_dots")
|
response = DomainHelper._return_form_error_or_json_response(
|
||||||
except errors.DomainUnavailableError:
|
error_return_type, code=error_map.get(type(error))
|
||||||
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")
|
|
||||||
else:
|
|
||||||
if display_success:
|
|
||||||
return DomainHelper._return_form_error_or_json_response(
|
|
||||||
error_return_type, code="success", available=True
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return validated
|
response = DomainHelper._return_form_error_or_json_response(
|
||||||
|
error_return_type, code="success", available=True
|
||||||
|
)
|
||||||
|
return (validated, response)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _return_form_error_or_json_response(return_type: ValidationErrorReturnType, code, available=False):
|
def _return_form_error_or_json_response(return_type: ValidationErrorReturnType, code, available=False):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue