Skip to content

Commit

Permalink
Improve function and variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
cuu508 committed Sep 7, 2023
1 parent 18aac7f commit a2fa62c
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions hc/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a2fa62c

Please sign in to comment.