# Buggy code
def _check_and_get_response(response: Response, method: str) -> Union[str, Dict]:
    """Checks the kraken response and if it's succesfull returns the result.

    If there is recoverable error a string is returned explaining the error
    May raise:
    - RemoteError if there is an unrecoverable/unexpected remote error
    """
    if response.status_code in (520, 525, 504):
        log.debug(f"Kraken returned status code {response.status_code}")
        return "Usual kraken 5xx shenanigans"
    elif response.status_code != 200:
        raise RemoteError(
            "Kraken API request {} for {} failed with HTTP status code: {}".format(
                response.url,
                method,
                response.status_code,
            )
        )

    result = rlk_jsonloads_dict(response.text)
    if result["error"]:
        if isinstance(result["error"], list):
            error = result["error"][0]
        else:
            error = result["error"]

        if "Rate limit exceeded" in error:
            log.debug(f"Kraken: Got rate limit exceeded error: {error}")
            return "Rate limited exceeded"
        else:
            raise RemoteError(error)

    return result["result"]


# Traceback (KeyError)
27/04/2020 17:06:16 W. Europe Daylight Time -- INFO:rotkehlchen.exchanges.exchange:Initialized kraken exchange
27/04/2020 17:06:16 W. Europe Daylight Time -- DEBUG:rotkehlchen.exchanges.kraken:Kraken API query method=Balance, data=None, call_counter=0
27/04/2020 17:06:16 W. Europe Daylight Time -- ERROR:rotkehlchen.api.server:Exception on /api/1/exchanges [PUT]
Traceback (most recent call last):
File "C:\Users\Gebruiker\Envs\rotkehlchen\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Gebruiker\Envs\rotkehlchen\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Gebruiker\Envs\rotkehlchen\lib\site-packages\flask_restful\__init__.py", line 458, in wrapper
resp = resource(*args, **kwargs)
File "C:\Users\Gebruiker\Envs\rotkehlchen\lib\site-packages\flask\views.py", line 89, in view
return self.dispatch_request(*args, **kwargs)
File "C:\Users\Gebruiker\Envs\rotkehlchen\lib\site-packages\flask_restful\__init__.py", line 573, in dispatch_request
resp = meth(*args, **kwargs)
File "C:\Users\Gebruiker\Envs\rotkehlchen\lib\site-packages\webargs\core.py", line 366, in wrapper
return func(*args, **kwargs)
File "c:\users\gebruiker\source\repos\rotki\rotkehlchen\api\v1\resources.py", line 215, in put
return self.rest_api.setup_exchange(name, api_key, api_secret, passphrase)
File "c:\users\gebruiker\source\repos\rotki\rotkehlchen\api\rest.py", line 109, in wrapper
return f(wrappingobj, *args, **kwargs)
File "c:\users\gebruiker\source\repos\rotki\rotkehlchen\api\rest.py", line 377, in setup_exchange
result, message = self.rotkehlchen.setup_exchange(name, api_key, api_secret, passphrase)
File "c:\users\gebruiker\source\repos\rotki\rotkehlchen\rotkehlchen.py", line 635, in setup_exchange
passphrase=passphrase,
File "c:\users\gebruiker\source\repos\rotki\rotkehlchen\exchanges\manager.py", line 78, in setup_exchange
result, message = exchange.validate_api_key()
File "c:\users\gebruiker\source\repos\rotki\rotkehlchen\exchanges\kraken.py", line 318, in validate_api_key
valid, msg = self._validate_single_api_key_action('Balance')
File "c:\users\gebruiker\source\repos\rotki\rotkehlchen\exchanges\kraken.py", line 341, in _validate_single_api_key_action
self.api_query(method_str, req)
File "c:\users\gebruiker\source\repos\rotki\rotkehlchen\exchanges\kraken.py", line 419, in api_query
result = query_method(method, req)
File "c:\users\gebruiker\source\repos\rotki\rotkehlchen\exchanges\kraken.py", line 471, in _query_private
return _check_and_get_response(response, method)
File "c:\users\gebruiker\source\repos\rotki\rotkehlchen\exchanges\kraken.py", line 246, in _check_and_get_response
return result['result']
KeyError: 'result'

... (and later if i try to register it again I get this)

27/04/2020 17:06:33 W. Europe Daylight Time -- INFO:rotkehlchen.api.server.pywsgi:127.0.0.1 - - [2020-04-27 17:06:33] "OPTIONS /api/1/exchanges HTTP/1.1" 200 327 0.001009
27/04/2020 17:06:33 W. Europe Daylight Time -- DEBUG:rotkehlchen.api.rest:Request successful response={"result": null, "message": "Exchange kraken is already registered"}, status_code=409

# Fixed code
def _check_and_get_response(response: Response, method: str) -> Union[str, Dict]:
    """Checks the kraken response and if it's succesfull returns the result.

    If there is recoverable error a string is returned explaining the error
    May raise:
    - RemoteError if there is an unrecoverable/unexpected remote error
    """
    if response.status_code in (520, 525, 504):
        log.debug(f"Kraken returned status code {response.status_code}")
        return "Usual kraken 5xx shenanigans"
    elif response.status_code != 200:
        raise RemoteError(
            "Kraken API request {} for {} failed with HTTP status code: {}".format(
                response.url,
                method,
                response.status_code,
            )
        )

    try:
        decoded_json = rlk_jsonloads_dict(response.text)
    except json.decoder.JSONDecodeError as e:
        raise RemoteError(f"Invalid JSON in Kraken response. {e}")

    try:
        if decoded_json["error"]:
            if isinstance(decoded_json["error"], list):
                error = decoded_json["error"][0]
            else:
                error = decoded_json["error"]

            if "Rate limit exceeded" in error:
                log.debug(f"Kraken: Got rate limit exceeded error: {error}")
                return "Rate limited exceeded"
            else:
                raise RemoteError(error)

        result = decoded_json["result"]
    except KeyError as e:
        raise RemoteError(f"Unexpected format of Kraken response. Missing key: {e}")

    return result