Skip to content

Commit

Permalink
[#3607] Use better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Dec 6, 2023
1 parent cc590b7 commit 195e5e7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ interactions:
uri: http://localhost:8150/kadastraalonroerendezaken?postcode=wrong&huisnummer=wrong
response:
body:
string: '{"detail":"Not Found"}'
string: '{"_embedded":{"kadastraalOnroerendeZaken":[]}}'
headers:
content-length:
- '22'
- '46'
content-type:
- application/json
date:
- Tue, 05 Dec 2023 17:40:19 GMT
- Wed, 06 Dec 2023 10:53:20 GMT
server:
- uvicorn
status:
code: 404
message: Not Found
code: 200
message: OK
- request:
body: null
headers:
Expand Down Expand Up @@ -59,7 +59,7 @@ interactions:
content-type:
- application/json
date:
- Tue, 05 Dec 2023 17:40:19 GMT
- Wed, 06 Dec 2023 10:53:20 GMT
server:
- uvicorn
status:
Expand Down Expand Up @@ -91,7 +91,7 @@ interactions:
content-type:
- application/json
date:
- Tue, 05 Dec 2023 17:40:19 GMT
- Wed, 06 Dec 2023 10:53:20 GMT
server:
- uvicorn
status:
Expand Down Expand Up @@ -125,7 +125,7 @@ interactions:
content-type:
- application/json
date:
- Tue, 05 Dec 2023 17:40:19 GMT
- Wed, 06 Dec 2023 10:53:20 GMT
server:
- uvicorn
status:
Expand Down Expand Up @@ -157,7 +157,7 @@ interactions:
content-type:
- application/json
date:
- Tue, 05 Dec 2023 17:40:19 GMT
- Wed, 06 Dec 2023 10:53:20 GMT
server:
- uvicorn
status:
Expand Down Expand Up @@ -191,7 +191,7 @@ interactions:
content-type:
- application/json
date:
- Tue, 05 Dec 2023 17:40:19 GMT
- Wed, 06 Dec 2023 10:53:20 GMT
server:
- uvicorn
status:
Expand Down Expand Up @@ -223,7 +223,7 @@ interactions:
content-type:
- application/json
date:
- Tue, 05 Dec 2023 17:40:19 GMT
- Wed, 06 Dec 2023 10:53:20 GMT
server:
- uvicorn
status:
Expand Down
7 changes: 4 additions & 3 deletions src/openforms/contrib/brk/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_brk_validator(self):
)

with self.assertRaisesMessage(
ValidationError, _("%(type)s does not exist.") % {"type": _("Owner")}
ValidationError, _("No BSN is available to validate your address.")
):
validator(
{"postcode": "not_relevant", "houseNumber": "same"}, submission_no_bsn
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_brk_validator(self):
)

with self.assertRaisesMessage(
ValidationError, _("%(type)s does not exist.") % {"type": _("Owner")}
ValidationError, _("No property found for this address.")
):
validator(
{"postcode": "wrong", "houseNumber": "wrong"}, submission_not_hashed
Expand All @@ -76,7 +76,8 @@ def test_brk_validator(self):
validator({"postcode": "1234AA", "houseNumber": "123"}, submission_hashed)

with self.assertRaisesMessage(
ValidationError, _("%(type)s does not exist.") % {"type": _("Owner")}
ValidationError,
_("You are not one of the registered owners of this property"),
):
validator(
{"postcode": "1234AA", "houseNumber": "123"}, submission_wrong_bsn
Expand Down
31 changes: 17 additions & 14 deletions src/openforms/contrib/brk/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

class AddressValue(TypedDict, total=False):
postcode: str
housenumber: str
houseletter: str
housenumberaddition: str
houseNumber: str
houseLetter: str
houseNumberAddition: str


@register(
Expand All @@ -30,20 +30,21 @@ class AddressValue(TypedDict, total=False):
class BRKZaakgerechtigdeValidator:

error_messages = {
"not_found": _("%(type)s does not exist."),
"no_bsn": _("No BSN is available to validate your address."),
"retrieving_error": _(
"There was an error while retrieving the available properties. Please try again later"
),
"not_owner": _("You are not one of the registered owners of this property"),
"multiple_cadastrals": _("We couldn't accurately determine your properties"),
"not_found": _("No property found for this address."),
}

def __call__(self, value: AddressValue, submission: Submission) -> bool:

validation_error = ValidationError(
self.error_messages["not_found"],
params={"type": _("Owner")},
)

try:
client = get_client()
except NoServiceConfigured as e:
raise validation_error from e
raise ValidationError(self.error_messages["retrieving_error"]) from e

address_query: SearchParams = {
"postcode": value["postcode"],
Expand All @@ -57,15 +58,17 @@ def __call__(self, value: AddressValue, submission: Submission) -> bool:
# We assume submission has auth_info available, should we verify that this validator is used
# on a step that requires login?
if submission.auth_info.attribute != AuthAttribute.bsn:
raise validation_error
raise ValidationError(self.error_messages["no_bsn"])

try:
with client:
cadastrals_resp = client.get_cadastrals_by_address(address_query)
kadastraals = cadastrals_resp["_embedded"]["kadastraalOnroerendeZaken"]
if len(kadastraals) > 1:
# The query by address returned more than one cadastral, this shouldn't happen
raise validation_error
raise ValidationError(self.error_messages["multiple_cadastrals"])
if not kadastraals:
raise ValidationError(self.error_messages["not_found"])
kadastraal_id = kadastraals[0]["identificatie"]

titleholders_resp = client.get_cadastral_titleholders_by_cadastral_id(
Expand All @@ -85,9 +88,9 @@ def __call__(self, value: AddressValue, submission: Submission) -> bool:
is_valid = submission.auth_info.value in bsns

if not is_valid:
raise validation_error
raise ValidationError(self.error_messages["not_owner"])

except (RequestException, KeyError, TypeError) as e:
raise validation_error from e
raise ValidationError(self.error_messages["retrieving_error"]) from e

return True

0 comments on commit 195e5e7

Please sign in to comment.