summary refs log tree commit diff
path: root/synapse/http
diff options
context:
space:
mode:
authorDavid Robertson <davidr@element.io>2022-09-07 12:16:10 +0100
committerGitHub <noreply@github.com>2022-09-07 12:16:10 +0100
commitb58386e37e30e920332e4b04011b528a66a39fad (patch)
treee1fc845196b649e8819aee5fe002a4331c4f6c2f /synapse/http
parentCancel the processing of key query requests when they time out. (#13680) (diff)
downloadsynapse-b58386e37e30e920332e4b04011b528a66a39fad.tar.xz
A second batch of Pydantic models for rest/client/account.py (#13687)
Diffstat (limited to 'synapse/http')
-rw-r--r--synapse/http/servlet.py19
1 files changed, 17 insertions, 2 deletions
diff --git a/synapse/http/servlet.py b/synapse/http/servlet.py

index 26aaabfb34..80acbdcf3c 100644 --- a/synapse/http/servlet.py +++ b/synapse/http/servlet.py
@@ -28,7 +28,8 @@ from typing import ( overload, ) -from pydantic import BaseModel, ValidationError +from pydantic import BaseModel, MissingError, PydanticValueError, ValidationError +from pydantic.error_wrappers import ErrorWrapper from typing_extensions import Literal from twisted.web.server import Request @@ -714,7 +715,21 @@ def parse_and_validate_json_object_from_request( try: instance = model_type.parse_obj(content) except ValidationError as e: - raise SynapseError(HTTPStatus.BAD_REQUEST, str(e), errcode=Codes.BAD_JSON) + # Choose a matrix error code. The catch-all is BAD_JSON, but we try to find a + # more specific error if possible (which occasionally helps us to be spec- + # compliant) This is a bit awkward because the spec's error codes aren't very + # clear-cut: BAD_JSON arguably overlaps with MISSING_PARAM and INVALID_PARAM. + errcode = Codes.BAD_JSON + + raw_errors = e.raw_errors + if len(raw_errors) == 1 and isinstance(raw_errors[0], ErrorWrapper): + raw_error = raw_errors[0].exc + if isinstance(raw_error, MissingError): + errcode = Codes.MISSING_PARAM + elif isinstance(raw_error, PydanticValueError): + errcode = Codes.INVALID_PARAM + + raise SynapseError(HTTPStatus.BAD_REQUEST, str(e), errcode=errcode) return instance