mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2024-12-29 08:32:08 +00:00
修复部分情况下提取 serializer 的错误信息的错误
This commit is contained in:
parent
6b7654a0c3
commit
6ef16c8253
@ -89,20 +89,22 @@ class APIView(View):
|
||||
def error(self, msg="error", err="error"):
|
||||
return self.response({"error": err, "data": msg})
|
||||
|
||||
def _serializer_error_to_str(self, errors):
|
||||
for k, v in errors.items():
|
||||
if isinstance(v, list):
|
||||
return k, v[0]
|
||||
elif isinstance(v, OrderedDict):
|
||||
for _k, _v in v.items():
|
||||
return self._serializer_error_to_str({_k: _v})
|
||||
def extract_errors(self, errors, key="field"):
|
||||
if isinstance(errors, dict):
|
||||
if not errors:
|
||||
return key, "Invalid field"
|
||||
key = list(errors.keys())[0]
|
||||
return self.extract_errors(errors.pop(key), key)
|
||||
elif isinstance(errors, list):
|
||||
return self.extract_errors(errors[0], key)
|
||||
|
||||
def invalid_serializer(self, serializer):
|
||||
k, v = self._serializer_error_to_str(serializer.errors)
|
||||
if k != "non_field_errors":
|
||||
return self.error(err="invalid-" + k, msg=k + ": " + v)
|
||||
key, error = self.extract_errors(serializer.errors)
|
||||
if key == "non_field_errors":
|
||||
msg = error
|
||||
else:
|
||||
return self.error(err="invalid-field", msg=v)
|
||||
msg = f"{key}: {error}"
|
||||
return self.error(err=f"invalid-{key}", msg=msg)
|
||||
|
||||
def server_error(self):
|
||||
return self.error(err="server-error", msg="server error")
|
||||
|
Loading…
Reference in New Issue
Block a user