Skip to content
Snippets Groups Projects
Commit fcacaba9 authored by inso's avatar inso
Browse files

Catch ucoin errors

parent bc6fa65c
Branches
Tags
No related merge requests found
...@@ -26,5 +26,5 @@ import logging ...@@ -26,5 +26,5 @@ import logging
logger = logging.getLogger("ucoin") logger = logging.getLogger("ucoin")
from .api import API, ConnectionHandler from .api import API, ConnectionHandler, UcoinError
from . import network, blockchain, tx, wot, node, ud, ws from . import network, blockchain, tx, wot, node, ud, ws
\ No newline at end of file
...@@ -23,6 +23,15 @@ import aiohttp, json, logging, jsonschema ...@@ -23,6 +23,15 @@ import aiohttp, json, logging, jsonschema
logger = logging.getLogger("ucoin") logger = logging.getLogger("ucoin")
class UcoinError(Exception):
"""
UCoin error
"""
def __init__(self, data):
super().__init__("Error code {0} - {1}".format(data["ucode"], data["message"]))
self.error = data
class ConnectionHandler(object): class ConnectionHandler(object):
"""Helper class used by other API classes to ease passing server connection information.""" """Helper class used by other API classes to ease passing server connection information."""
...@@ -162,6 +171,10 @@ class API(object): ...@@ -162,6 +171,10 @@ class API(object):
with aiohttp.Timeout(15): with aiohttp.Timeout(15):
response = await session.get(self.reverse_url("http", path), params=kwargs,headers=self.headers) response = await session.get(self.reverse_url("http", path), params=kwargs,headers=self.headers)
if response.status != 200: if response.status != 200:
try:
error_data = self.parse_error(await response.text())
raise UcoinError(error_data)
except TypeError:
raise ValueError('status code != 200 => %d (%s)' % (response.status, (await response.text()))) raise ValueError('status code != 200 => %d (%s)' % (response.status, (await response.text())))
return response return response
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment