Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Make errors configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Jul 25, 2022
1 parent 536b14d commit fa91feb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
7 changes: 5 additions & 2 deletions synapse/api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,14 @@ def __init__(
self.previous_errcode = previous_errcode
super().__init__(code, msg, errcode, additional_fields)

def error_dict(self) -> "JsonDict":
def error_dict(self, allow_unstable_fields: bool = False) -> "JsonDict":
fields = {}
if allow_unstable_fields:
fields["org.matrix.msc3848.unstable.errcode"] = self.errcode
return cs_error(
self.msg,
self.previous_errcode,
**{"org.matrix.msc3848.unstable.errcode": self.errcode},
**fields,
**self._additional_fields,
)

Expand Down
3 changes: 3 additions & 0 deletions synapse/config/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,6 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:

# MSC3827: Filtering of /publicRooms by room type
self.msc3827_enabled: bool = experimental.get("msc3827_enabled", False)

# MSC3848: Introduce errcodes for specific event sending failures
self.msc3848_enabled: bool = experimental.get("msc3848_enabled", False)
8 changes: 7 additions & 1 deletion synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
IncompatibleRoomVersionError,
NotFoundError,
SynapseError,
UnstableSpecAuthError,
UnsupportedRoomVersionError,
)
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersion
Expand Down Expand Up @@ -468,7 +469,12 @@ async def process_pdus_for_room(room_id: str) -> None:
)
for pdu in pdus_by_room[room_id]:
event_id = pdu.event_id
pdu_results[event_id] = e.error_dict()
if isinstance(e, UnstableSpecAuthError):
pdu_results[event_id] = e.error_dict(
self.hs.config.experimental.msc3848_enabled
)
else:
pdu_results[event_id] = e.error_dict()
return

for pdu in pdus_by_room[room_id]:
Expand Down
3 changes: 3 additions & 0 deletions synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
LoginError,
StoreError,
SynapseError,
UnstableSpecAuthError,
UserDeactivatedError,
)
from synapse.api.ratelimiting import Ratelimiter
Expand Down Expand Up @@ -562,6 +563,8 @@ async def check_ui_auth(
await self.store.mark_ui_auth_stage_complete(
session.session_id, login_type, result
)
except UnstableSpecAuthError as e:
errordict = e.error_dict(self.hs.config.experimental.msc3848_enabled)
except LoginError as e:
# this step failed. Merge the error dict into the response
# so that the client can have another go.
Expand Down
19 changes: 17 additions & 2 deletions synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
RedirectException,
SynapseError,
UnrecognizedRequestError,
UnstableSpecAuthError,
)
from synapse.http.site import SynapseRequest
from synapse.logging.context import defer_to_thread, preserve_fn, run_in_background
Expand Down Expand Up @@ -155,14 +156,20 @@ def is_method_cancellable(method: Callable[..., Any]) -> bool:
return getattr(method, "cancellable", False)


def return_json_error(f: failure.Failure, request: SynapseRequest) -> None:
def return_json_error(
f: failure.Failure, request: SynapseRequest, allow_unstable_fields=False
) -> None:
"""Sends a JSON error response to clients."""

if f.check(SynapseError):
# mypy doesn't understand that f.check asserts the type.
exc: SynapseError = f.value # type: ignore
error_code = exc.code
error_dict = exc.error_dict()
if f.check(UnstableSpecAuthError):
unstable_exc: UnstableSpecAuthError = f.value # type: ignore
error_dict = unstable_exc.error_dict(allow_unstable_fields)
else:
error_dict = exc.error_dict()

logger.info("%s SynapseError: %s - %s", request, error_code, exc.msg)
elif f.check(CancelledError):
Expand Down Expand Up @@ -575,6 +582,14 @@ async def _async_render(self, request: SynapseRequest) -> Tuple[int, Any]:

return callback_return

def _send_error_response(
self,
f: failure.Failure,
request: SynapseRequest,
) -> None:
"""Implements _AsyncResource._send_error_response"""
return_json_error(f, request, self.hs.config.experimental.msc3848_enabled)


class DirectServeHtmlResource(_AsyncResource):
"""A resource that will call `self._async_on_<METHOD>` on new requests,
Expand Down

0 comments on commit fa91feb

Please sign in to comment.