# Buggy code
async def on_PUT(self, request, user_id):
    requester = await self.auth.get_user_by_req(request)
    await assert_user_is_admin(self.auth, requester.user)
    auth_user = requester.user

    target_user = UserID.from_string(user_id)

    body = parse_json_object_from_request(request)

    assert_params_in_dict(body, ["admin"])

    if not self.hs.is_mine(target_user):
        raise SynapseError(400, "Only local users can be admins of this homeserver")

    set_admin_to = bool(body["admin"])

    if target_user == auth_user and not set_admin_to:
        raise SynapseError(400, "You may not demote yourself.")

    await self.store.set_user_server_admin(target_user, set_admin_to)

    return 200, {}


# Traceback (AttributeError)
2020-02-13 19:15:53,449 - synapse.http.server - 110 - ERROR - PUT-31 - Failed handle request via 'UserRestServletV2': <SynapseRequest at 0x7f6a6035bfd0 method='PUT' uri='/_synapse/admin/v2/users/@mjolnir-dev:redacted' clientproto='HTTP/1.0' site=8008>
Traceback (most recent call last):
File "/home/matrix/.synapse/local/lib/python3.6/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
StopIteration: {'displayname': 'mjolnir-dev', 'avatar_url': None}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/matrix/.synapse/local/lib/python3.6/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
StopIteration: {'displayname': 'mjolnir-dev', 'avatar_url': None}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/matrix/.synapse/local/lib/python3.6/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
StopIteration: ProfileInfo(avatar_url=None, display_name='mjolnir-dev')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/matrix/.synapse/local/lib/python3.6/site-packages/synapse/http/server.py", line 78, in wrapped_request_handler
await h(self, request)
File "/home/matrix/.synapse/local/lib/python3.6/site-packages/synapse/http/server.py", line 331, in _async_render
callback_return = await callback_return
File "/home/matrix/.synapse/local/lib/python3.6/site-packages/synapse/rest/admin/users.py", line 185, in on_PUT
await self.admin_handler.set_user_server_admin(
AttributeError: 'AdminHandler' object has no attribute 'set_user_server_admin'

# Fixed code
async def on_PUT(self, request, user_id):
    requester = await self.auth.get_user_by_req(request)
    await assert_user_is_admin(self.auth, requester.user)
    auth_user = requester.user

    target_user = UserID.from_string(user_id)

    body = parse_json_object_from_request(request)

    assert_params_in_dict(body, ["admin"])

    if not self.hs.is_mine(target_user):
        raise SynapseError(400, "Only local users can be admins of this homeserver")

    set_admin_to = bool(body["admin"])

    if target_user == auth_user and not set_admin_to:
        raise SynapseError(400, "You may not demote yourself.")

    await self.store.set_server_admin(target_user, set_admin_to)

    return 200, {}