Skip to content

Commit

Permalink
Add log of URL on HTTP errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
gertjanklein committed Jul 2, 2020
1 parent 9d68ca2 commit 895b15b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
27 changes: 19 additions & 8 deletions copy-iris-items.pyw
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,13 @@ def get_modified_items(config, itemtype):
url = f"{scheme}://{svr.host}:{svr.port}/api/atelier/v1/{svr.namespace}/modified/{itemtype}?generated=0"
rq = urq.Request(url, data=b'[]', headers={'Content-Type': 'application/json'}, method='POST')

# Get and convert to JSON
with urq.urlopen(rq) as rsp:
data = json.load(rsp)
try:
# Get and convert to JSON
with urq.urlopen(rq) as rsp:
data = json.load(rsp)
except urq.URLError:
logging.error(f"Accessing {url}:")
raise

return data

Expand All @@ -107,8 +111,12 @@ def get_items_for_type(config, itemtype):
url = f"{scheme}://{svr.host}:{svr.port}/api/atelier/v1/{svr.namespace}/docnames/{itemtype}"

# Get and convert to JSON
with urq.urlopen(url) as rsp:
data = json.load(rsp)
try:
with urq.urlopen(url) as rsp:
data = json.load(rsp)
except urq.URLError:
logging.error(f"Accessing {url}:")
raise

return data

Expand Down Expand Up @@ -200,9 +208,12 @@ def retrieve_item(config, item):
scheme = 'https' if svr.https else 'http'
url = f"{scheme}://{svr.host}:{svr.port}/api/atelier/v1/{svr.namespace}/doc/{name}"

rsp = urq.urlopen(url)
with rsp:
data = json.load(rsp)
try:
with urq.urlopen(url) as rsp:
data = json.load(rsp)
except urq.URLError:
logging.error(f"Accessing {url}:")
raise

# Contents may be returned line-by-line
content = data['result']['content']
Expand Down
44 changes: 34 additions & 10 deletions data_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ def get_export(svr, name:str):
query = f"SELECT Tmp_CII.GetExport('{name}') AS result"
data = json.dumps({ "query": query }).encode()
rq = urq.Request(url, data=data, headers={'Content-Type': 'application/json'}, method='POST')
with urq.urlopen(rq) as rsp:
data = json.load(rsp)

try:
with urq.urlopen(rq) as rsp:
data = json.load(rsp)
except urq.URLError:
logging.error(f"Accessing [POST] {url}:")
raise

# Check for errors
errors = data['status']['errors']
Expand Down Expand Up @@ -67,8 +72,12 @@ def list_lookup_tables(svr, specs:list):

# Run query
rq = urq.Request(url, data=data, headers={'Content-Type': 'application/json'}, method='POST')
with urq.urlopen(rq) as rsp:
data = json.load(rsp)
try:
with urq.urlopen(rq) as rsp:
data = json.load(rsp)
except urq.URLError:
logging.error(f"Accessing [POST] {url}:")
raise

# Check for errors
errors = data['status']['errors']
Expand All @@ -91,8 +100,13 @@ def init(svr):
query = "SELECT 1 FROM %Dictionary.ClassDefinition WHERE ID = 'Tmp.CII.Procs'"
data = json.dumps({ "query": query }).encode()
rq = urq.Request(url, data=data, headers={'Content-Type': 'application/json'}, method='POST')
with urq.urlopen(rq) as rsp:
data = json.load(rsp)

try:
with urq.urlopen(rq) as rsp:
data = json.load(rsp)
except urq.URLError:
logging.error(f"Accessing [POST] {url}:")
raise

# If the class still exists, we're done
if data['result']['content']:
Expand All @@ -101,8 +115,13 @@ def init(svr):
# Get the SQL to create the stored procedure
data = json.dumps({ "query": CREATE_EXPORT_PROC }).encode()
rq = urq.Request(url, data=data, headers={'Content-Type': 'application/json'}, method='POST')
with urq.urlopen(rq) as rsp:
data = json.load(rsp)

try:
with urq.urlopen(rq) as rsp:
data = json.load(rsp)
except urq.URLError:
logging.error(f"Accessing [POST] {url}:")
raise

errors = data['status']['errors']
if errors:
Expand All @@ -126,8 +145,13 @@ def cleanup(svr:dict):
query = "DROP PROCEDURE Tmp_CII.GetExport"
data = json.dumps({ "query": query }).encode()
rq = urq.Request(url, data=data, headers={'Content-Type': 'application/json'}, method='POST')
with urq.urlopen(rq) as rsp:
data = json.load(rsp)

try:
with urq.urlopen(rq) as rsp:
data = json.load(rsp)
except urq.URLError:
logging.error(f"Accessing [POST] {url}:")
raise

# If that returned errors, don't raise but do add a warning to the log
errors = data['status']['errors']
Expand Down

0 comments on commit 895b15b

Please sign in to comment.