Skip to content

Commit b86fe1f

Browse files
committed
Abstract out exception handling for tool shed API exceptions.
1 parent 46b2524 commit b86fe1f

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

planemo/commands/cmd_shed_upload.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""
22
"""
3-
import json
43
import sys
54

65
import click
@@ -96,20 +95,9 @@ def __handle_upload(ctx, realized_repository, **kwds):
9695
try:
9796
tsi.repositories.update_repository(repo_id, tar_path, **update_kwds)
9897
except Exception as e:
99-
if hasattr(e, "read"):
100-
exception_content = e.read()
101-
try:
102-
# Galaxy passes nice JSON messages as their errors, which bioblend
103-
# blindly returns. Attempt to parse those.
104-
upstream_error = json.loads(exception_content)
105-
error(upstream_error['err_msg'])
106-
except Exception as e2:
107-
error("Could not update %s" % realized_repository.name)
108-
error(exception_content)
109-
error(e2.read())
110-
else:
111-
error("Could not update %s" % realized_repository.name)
112-
error(str(e))
98+
message = shed.api_exception_to_message(e)
99+
error("Could not update %s" % realized_repository.name)
100+
error(message)
113101
return -1
114102
info("Repository %s updated successfully." % realized_repository.name)
115103
return 0

planemo/shed.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -788,13 +788,9 @@ def find_repository_id(self, ctx, tsi):
788788
)
789789
return repo_id
790790
except Exception as e:
791+
message = api_exception_to_message(e)
791792
error("Could not update %s" % self.name)
792-
try:
793-
error(e.read())
794-
except AttributeError:
795-
# I've seen a case where the error couldn't be read, so now
796-
# wrapped in try/except
797-
error("Could not query for repository in toolshed")
793+
error(message)
798794
return None
799795

800796
def create(self, ctx, tsi):
@@ -819,6 +815,20 @@ def create(self, ctx, tsi):
819815
return None
820816

821817

818+
def api_exception_to_message(e):
819+
message = str(e)
820+
if hasattr(e, "read"):
821+
message = e.read()
822+
try:
823+
# Galaxy passes nice JSON messages as their errors, which bioblend
824+
# blindly returns. Attempt to parse those.
825+
upstream_error = json.loads(message)
826+
message = upstream_error['err_msg']
827+
except Exception:
828+
pass
829+
return message
830+
831+
822832
def _glob(path, pattern):
823833
pattern = os.path.join(path, pattern)
824834
if os.path.isdir(pattern):

0 commit comments

Comments
 (0)