prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Uploading a too large file: wrong error code
  When uploading a too large file, synapse responds with a 413 (OK) but with errcode M_UNKNOWN. According to the [spec](https://matrix.org/docs/spec/client_server/latest#post-matrix-media-r0-upload), it should be "M_TOO_LARGE"
  
  Received responseCode: 413
  Received responseBody:
  `{"errcode":"M_UNKNOWN","error":"Upload request body is too large"}`
  
  
  
  </issue>
  <code>
  [start of synapse/rest/media/v1/upload_resource.py]
  1 # -*- coding: utf-8 -*-
  2 # Copyright 2014-2016 OpenMarket Ltd
  3 #
  4 # Licensed under the Apache License, Version 2.0 (the "License");
  5 # you may not use this file except in compliance with the License.
  6 # You may obtain a copy of the License at
  7 #
  8 #     http://www.apache.org/licenses/LICENSE-2.0
  9 #
  10 # Unless required by applicable law or agreed to in writing, software
  11 # distributed under the License is distributed on an "AS IS" BASIS,
  12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 # See the License for the specific language governing permissions and
  14 # limitations under the License.
  15 
  16 import logging
  17 
  18 from twisted.web.server import NOT_DONE_YET
  19 
  20 from synapse.api.errors import SynapseError
  21 from synapse.http.server import (
  22     DirectServeResource,
  23     respond_with_json,
  24     wrap_json_request_handler,
  25 )
  26 from synapse.http.servlet import parse_string
  27 
  28 logger = logging.getLogger(__name__)
  29 
  30 
  31 class UploadResource(DirectServeResource):
  32     isLeaf = True
  33 
  34     def __init__(self, hs, media_repo):
  35         super().__init__()
  36 
  37         self.media_repo = media_repo
  38         self.filepaths = media_repo.filepaths
  39         self.store = hs.get_datastore()
  40         self.clock = hs.get_clock()
  41         self.server_name = hs.hostname
  42         self.auth = hs.get_auth()
  43         self.max_upload_size = hs.config.max_upload_size
  44         self.clock = hs.get_clock()
  45 
  46     def render_OPTIONS(self, request):
  47         respond_with_json(request, 200, {}, send_cors=True)
  48         return NOT_DONE_YET
  49 
  50     @wrap_json_request_handler
  51     async def _async_render_POST(self, request):
  52         requester = await self.auth.get_user_by_req(request)
  53         # TODO: The checks here are a bit late. The content will have
  54         # already been uploaded to a tmp file at this point
  55         content_length = request.getHeader(b"Content-Length").decode("ascii")
  56         if content_length is None:
  57             raise SynapseError(msg="Request must specify a Content-Length", code=400)
  58         if int(content_length) > self.max_upload_size:
  59             raise SynapseError(msg="Upload request body is too large", code=413)
  60 
  61         upload_name = parse_string(request, b"filename", encoding=None)
  62         if upload_name:
  63             try:
  64                 upload_name = upload_name.decode("utf8")
  65             except UnicodeDecodeError:
  66                 raise SynapseError(
  67                     msg="Invalid UTF-8 filename parameter: %r" % (upload_name), code=400
  68                 )
  69 
  70         headers = request.requestHeaders
  71 
  72         if headers.hasHeader(b"Content-Type"):
  73             media_type = headers.getRawHeaders(b"Content-Type")[0].decode("ascii")
  74         else:
  75             raise SynapseError(msg="Upload request missing 'Content-Type'", code=400)
  76 
  77         # if headers.hasHeader(b"Content-Disposition"):
  78         #     disposition = headers.getRawHeaders(b"Content-Disposition")[0]
  79         # TODO(markjh): parse content-dispostion
  80 
  81         content_uri = await self.media_repo.create_content(
  82             media_type, upload_name, request.content, content_length, requester.user
  83         )
  84 
  85         logger.info("Uploaded content with URI %r", content_uri)
  86 
  87         respond_with_json(request, 200, {"content_uri": content_uri}, send_cors=True)
  88
  [end of synapse/rest/media/v1/upload_resource.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/synapse/rest/media/v1/upload_resource.py b/synapse/rest/media/v1/upload_resource.py
  --- a/synapse/rest/media/v1/upload_resource.py
  +++ b/synapse/rest/media/v1/upload_resource.py
  @@ -17,7 +17,7 @@
   
   from twisted.web.server import NOT_DONE_YET
   
  -from synapse.api.errors import SynapseError
  +from synapse.api.errors import Codes, SynapseError
   from synapse.http.server import (
       DirectServeResource,
       respond_with_json,
  @@ -56,7 +56,11 @@
           if content_length is None:
               raise SynapseError(msg="Request must specify a Content-Length", code=400)
           if int(content_length) > self.max_upload_size:
  -            raise SynapseError(msg="Upload request body is too large", code=413)
  +            raise SynapseError(
  +                msg="Upload request body is too large",
  +                code=413,
  +                errcode=Codes.TOO_LARGE,
  +            )
   
           upload_name = parse_string(request, b"filename", encoding=None)
           if upload_name:
  
verification_info: {"golden_diff": "diff --git a/synapse/rest/media/v1/upload_resource.py b/synapse/rest/media/v1/upload_resource.py\n--- a/synapse/rest/media/v1/upload_resource.py\n+++ b/synapse/rest/media/v1/upload_resource.py\n@@ -17,7 +17,7 @@\n \n from twisted.web.server import NOT_DONE_YET\n \n-from synapse.api.errors import SynapseError\n+from synapse.api.errors import Codes, SynapseError\n from synapse.http.server import (\n     DirectServeResource,\n     respond_with_json,\n@@ -56,7 +56,11 @@\n         if content_length is None:\n             raise SynapseError(msg=\"Request must specify a Content-Length\", code=400)\n         if int(content_length) > self.max_upload_size:\n-            raise SynapseError(msg=\"Upload request body is too large\", code=413)\n+            raise SynapseError(\n+                msg=\"Upload request body is too large\",\n+                code=413,\n+                errcode=Codes.TOO_LARGE,\n+            )\n \n         upload_name = parse_string(request, b\"filename\", encoding=None)\n         if upload_name:\n", "issue": "Uploading a too large file: wrong error code\nWhen uploading a too large file, synapse responds with a 413 (OK) but with errcode M_UNKNOWN. According to the [spec](https://matrix.org/docs/spec/client_server/latest#post-matrix-media-r0-upload), it should be \"M_TOO_LARGE\"\r\n\r\nReceived responseCode: 413\r\nReceived responseBody:\r\n`{\"errcode\":\"M_UNKNOWN\",\"error\":\"Upload request body is too large\"}`\r\n\r\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n# Copyright 2014-2016 OpenMarket Ltd\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport logging\n\nfrom twisted.web.server import NOT_DONE_YET\n\nfrom synapse.api.errors import SynapseError\nfrom synapse.http.server import (\n    DirectServeResource,\n    respond_with_json,\n    wrap_json_request_handler,\n)\nfrom synapse.http.servlet import parse_string\n\nlogger = logging.getLogger(__name__)\n\n\nclass UploadResource(DirectServeResource):\n    isLeaf = True\n\n    def __init__(self, hs, media_repo):\n        super().__init__()\n\n        self.media_repo = media_repo\n        self.filepaths = media_repo.filepaths\n        self.store = hs.get_datastore()\n        self.clock = hs.get_clock()\n        self.server_name = hs.hostname\n        self.auth = hs.get_auth()\n        self.max_upload_size = hs.config.max_upload_size\n        self.clock = hs.get_clock()\n\n    def render_OPTIONS(self, request):\n        respond_with_json(request, 200, {}, send_cors=True)\n        return NOT_DONE_YET\n\n    @wrap_json_request_handler\n    async def _async_render_POST(self, request):\n        requester = await self.auth.get_user_by_req(request)\n        # TODO: The checks here are a bit late. The content will have\n        # already been uploaded to a tmp file at this point\n        content_length = request.getHeader(b\"Content-Length\").decode(\"ascii\")\n        if content_length is None:\n            raise SynapseError(msg=\"Request must specify a Content-Length\", code=400)\n        if int(content_length) > self.max_upload_size:\n            raise SynapseError(msg=\"Upload request body is too large\", code=413)\n\n        upload_name = parse_string(request, b\"filename\", encoding=None)\n        if upload_name:\n            try:\n                upload_name = upload_name.decode(\"utf8\")\n            except UnicodeDecodeError:\n                raise SynapseError(\n                    msg=\"Invalid UTF-8 filename parameter: %r\" % (upload_name), code=400\n                )\n\n        headers = request.requestHeaders\n\n        if headers.hasHeader(b\"Content-Type\"):\n            media_type = headers.getRawHeaders(b\"Content-Type\")[0].decode(\"ascii\")\n        else:\n            raise SynapseError(msg=\"Upload request missing 'Content-Type'\", code=400)\n\n        # if headers.hasHeader(b\"Content-Disposition\"):\n        #     disposition = headers.getRawHeaders(b\"Content-Disposition\")[0]\n        # TODO(markjh): parse content-dispostion\n\n        content_uri = await self.media_repo.create_content(\n            media_type, upload_name, request.content, content_length, requester.user\n        )\n\n        logger.info(\"Uploaded content with URI %r\", content_uri)\n\n        respond_with_json(request, 200, {\"content_uri\": content_uri}, send_cors=True)\n", "path": "synapse/rest/media/v1/upload_resource.py"}]}