# Buggy code
def register_token(
    self, registry_address: TokenNetworkRegistryAddress, token_address: TokenAddress
) -> Response:
    if self.raiden_api.raiden.config.environment_type == Environment.PRODUCTION:
        return api_error(
            errors="Registering a new token is currently disabled in production mode",
            status_code=HTTPStatus.NOT_IMPLEMENTED,
        )

    conflict_exceptions = (
        AddressWithoutCode,
        AlreadyRegisteredTokenAddress,
        InvalidBinaryAddress,
        InvalidToken,
        InvalidTokenAddress,
        RaidenRecoverableError,
    )
    log.debug(
        "Registering token",
        node=to_checksum_address(self.raiden_api.address),
        registry_address=to_checksum_address(registry_address),
        token_address=to_checksum_address(token_address),
    )
    try:
        token_network_address = self.raiden_api.token_network_register(
            registry_address=registry_address,
            token_address=token_address,
            channel_participant_deposit_limit=TokenAmount(UINT256_MAX),
            token_network_deposit_limit=TokenAmount(UINT256_MAX),
        )
    except conflict_exceptions as e:
        return api_error(errors=str(e), status_code=HTTPStatus.CONFLICT)
    except InsufficientEth as e:
        return api_error(errors=str(e), status_code=HTTPStatus.PAYMENT_REQUIRED)

    return api_response(
        result=dict(token_network_address=to_checksum_address(token_network_address)),
        status_code=HTTPStatus.CREATED,
    )


# Traceback (raiden.exceptions.BrokenPreconditionError)
2020-01-29 18:24:57.713109 [info     ] 127.0.0.1 - - [2020-01-29 19:24:57] "PUT /api/v1/tokens/0x95B2d84De40a0121061b105E6B54016a49621B44 HTTP/1.1" 500 161 3.064439 [raiden.api.rest.pywsgi]
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 854, in gevent._greenlet.Greenlet.run
File "/home/raphael/code/brainbot/raiden/client/raiden/api/rest.py", line 439, in _run
self.wsgiserver.serve_forever()
File "/home/raphael/.virtualenvs/raiden/lib/python3.7/site-packages/gevent/baseserver.py", line 389, in serve_forever
self._stop_event.wait()
File "src/gevent/event.py", line 133, in gevent._event.Event.wait
File "src/gevent/_abstract_linkable.py", line 208, in gevent.__abstract_linkable.AbstractLinkable._wait
File "src/gevent/_abstract_linkable.py", line 178, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_abstract_linkable.py", line 196, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_abstract_linkable.py", line 182, in gevent.__abstract_linkable.AbstractLinkable._wait_core
File "src/gevent/_greenlet_primitives.py", line 61, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 61, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/_greenlet_primitives.py", line 65, in gevent.__greenlet_primitives.SwitchOutGreenletWithLoop.switch
File "src/gevent/__greenlet_primitives.pxd", line 35, in gevent.__greenlet_primitives._greenlet_switch
raiden.exceptions.BrokenPreconditionError: Number of token networks will exceed the max of 5
2020-01-29T18:24:57Z <Greenlet "APIServer|Greenlet-3" at 0x7f945954b4d0: _run> failed with BrokenPreconditionError

FATAL: An unexpected exception occured. A traceback has been written to /tmp/raiden-exception-2020-01-29T18-2580ljbh9m.txt
Number of token networks will exceed the max of 5
ReturnCode.FATAL

# Fixed code
def register_token(
    self, registry_address: TokenNetworkRegistryAddress, token_address: TokenAddress
) -> Response:
    if self.raiden_api.raiden.config.environment_type == Environment.PRODUCTION:
        return api_error(
            errors="Registering a new token is currently disabled in production mode",
            status_code=HTTPStatus.NOT_IMPLEMENTED,
        )

    conflict_exceptions = (
        AddressWithoutCode,
        AlreadyRegisteredTokenAddress,
        InvalidBinaryAddress,
        InvalidToken,
        InvalidTokenAddress,
        RaidenRecoverableError,
    )
    log.debug(
        "Registering token",
        node=to_checksum_address(self.raiden_api.address),
        registry_address=to_checksum_address(registry_address),
        token_address=to_checksum_address(token_address),
    )
    try:
        token_network_address = self.raiden_api.token_network_register(
            registry_address=registry_address,
            token_address=token_address,
            channel_participant_deposit_limit=TokenAmount(UINT256_MAX),
            token_network_deposit_limit=TokenAmount(UINT256_MAX),
        )
    except conflict_exceptions as e:
        return api_error(errors=str(e), status_code=HTTPStatus.CONFLICT)
    except InsufficientEth as e:
        return api_error(errors=str(e), status_code=HTTPStatus.PAYMENT_REQUIRED)
    except MaxTokenNetworkNumberReached as e:
        return api_error(errors=str(e), status_code=HTTPStatus.FORBIDDEN)

    return api_response(
        result=dict(token_network_address=to_checksum_address(token_network_address)),
        status_code=HTTPStatus.CREATED,
    )