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

Commit

Permalink
Return JSON errors instead of HTML errors for unknown resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Dec 17, 2021
1 parent b370d63 commit a7eb946
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 11 additions & 0 deletions synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,17 @@ def getChildWithDefault(self, path: str, request: Request) -> resource.Resource:
return super().getChildWithDefault(path, request)


class MissingResource(resource.Resource):
"""Similar to resource.NoResource, but returns a JSON error with proper CORS headers."""

def render(self, request: SynapseRequest) -> int:
return_json_error(failure.Failure(UnrecognizedRequestError()), request)
return NOT_DONE_YET

def getChild(self, name: str, request: Request) -> resource.Resource:
return self


class RootOptionsRedirectResource(OptionsResource, RootRedirect):
pass

Expand Down
6 changes: 4 additions & 2 deletions synapse/util/httpresourcetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import logging
from typing import Dict

from twisted.web.resource import NoResource, Resource
from twisted.web.resource import Resource

from synapse.http.server import MissingResource

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -49,7 +51,7 @@ def create_resource_tree(
for path_seg in full_path.split(b"/")[1:-1]:
if path_seg not in last_resource.listNames():
# resource doesn't exist, so make a "dummy resource"
child_resource: Resource = NoResource()
child_resource: Resource = MissingResource()
last_resource.putChild(path_seg, child_resource)
res_id = _resource_id(last_resource, path_seg)
resource_mappings[res_id] = child_resource
Expand Down

0 comments on commit a7eb946

Please sign in to comment.