Exceptions: fix types.

master
Tom Hacohen 4 years ago
parent 3894fd205b
commit 43dede57d2

@ -2,6 +2,7 @@ from fastapi import status
import typing as t import typing as t
from pydantic import BaseModel from pydantic import BaseModel
from django.core.exceptions import ValidationError as DjangoValidationError
class HttpErrorField(BaseModel): class HttpErrorField(BaseModel):
@ -92,8 +93,8 @@ class ValidationError(HttpError):
super().__init__(code=code, detail=detail, errors=errors, status_code=status_code) super().__init__(code=code, detail=detail, errors=errors, status_code=status_code)
def flatten_errors(field_name, errors) -> t.List[HttpError]: def flatten_errors(field_name: str, errors) -> t.List[HttpError]:
ret = [] ret: t.List[HttpError] = []
if isinstance(errors, dict): if isinstance(errors, dict):
for error_key in errors: for error_key in errors:
error = errors[error_key] error = errors[error_key]
@ -104,11 +105,11 @@ def flatten_errors(field_name, errors) -> t.List[HttpError]:
message = error.messages[0] message = error.messages[0]
else: else:
message = str(error) message = str(error)
ret.append(dict(code=error.code, detail=message, field=field_name)) ret.append(ValidationError(code=error.code, detail=message, field=field_name))
return ret return ret
def transform_validation_error(prefix, err): def transform_validation_error(prefix: str, err: DjangoValidationError):
if hasattr(err, "error_dict"): if hasattr(err, "error_dict"):
errors = flatten_errors(prefix, err.error_dict) errors = flatten_errors(prefix, err.error_dict)
elif not hasattr(err, "message"): elif not hasattr(err, "message"):

Loading…
Cancel
Save