From a2fa62c7be99e0afbcd9fb783032ae8994788e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 7 Sep 2023 14:05:47 +0300 Subject: [PATCH] Improve function and variable names --- hc/api/views.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/hc/api/views.py b/hc/api/views.py index fed0af6f7f4..5c63c450635 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -128,16 +128,14 @@ def check_schedule(cls, v: str) -> str: } -def format_error(exc: ValidationError) -> str: - for e in exc.errors(): - field = e["loc"][0] - if len(e["loc"]) == 2: - field = f"an item in '{field}'" +def format_first_error(exc: ValidationError) -> str: + first_error = exc.errors()[0] + subject = first_error["loc"][0] + if len(first_error["loc"]) == 2: + subject = f"an item in '{subject}'" - kind = e["type"] - return "json validation error: " + CUSTOM_ERRORS[kind] % field - - return "" + tmpl = CUSTOM_ERRORS[first_error["type"]] + return "json validation error: " + tmpl % subject @csrf_exempt @@ -373,7 +371,7 @@ def create_check(request): try: spec = Spec.model_validate(request.json, strict=True) except ValidationError as e: - return JsonResponse({"error": format_error(e)}, status=400) + return JsonResponse({"error": format_first_error(e)}, status=400) created = False check = _lookup(request.project, spec) @@ -439,7 +437,7 @@ def update_check(request, code): try: spec = Spec.model_validate(request.json, strict=True) except ValidationError as e: - return JsonResponse({"error": format_error(e)}, status=400) + return JsonResponse({"error": format_first_error(e)}, status=400) try: _update(check, spec, request.v)