Skip to content

Commit

Permalink
Abstract out exception handling for tool shed API exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmchilton committed Apr 30, 2015
1 parent 46b2524 commit b86fe1f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
18 changes: 3 additions & 15 deletions planemo/commands/cmd_shed_upload.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""
"""
import json
import sys

import click
Expand Down Expand Up @@ -96,20 +95,9 @@ def __handle_upload(ctx, realized_repository, **kwds):
try:
tsi.repositories.update_repository(repo_id, tar_path, **update_kwds)
except Exception as e:
if hasattr(e, "read"):
exception_content = e.read()
try:
# Galaxy passes nice JSON messages as their errors, which bioblend
# blindly returns. Attempt to parse those.
upstream_error = json.loads(exception_content)
error(upstream_error['err_msg'])
except Exception as e2:
error("Could not update %s" % realized_repository.name)
error(exception_content)
error(e2.read())
else:
error("Could not update %s" % realized_repository.name)
error(str(e))
message = shed.api_exception_to_message(e)
error("Could not update %s" % realized_repository.name)
error(message)
return -1
info("Repository %s updated successfully." % realized_repository.name)
return 0
22 changes: 16 additions & 6 deletions planemo/shed.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,13 +788,9 @@ def find_repository_id(self, ctx, tsi):
)
return repo_id
except Exception as e:
message = api_exception_to_message(e)
error("Could not update %s" % self.name)
try:
error(e.read())
except AttributeError:
# I've seen a case where the error couldn't be read, so now
# wrapped in try/except
error("Could not query for repository in toolshed")
error(message)
return None

def create(self, ctx, tsi):
Expand All @@ -819,6 +815,20 @@ def create(self, ctx, tsi):
return None


def api_exception_to_message(e):
message = str(e)
if hasattr(e, "read"):
message = e.read()
try:
# Galaxy passes nice JSON messages as their errors, which bioblend
# blindly returns. Attempt to parse those.
upstream_error = json.loads(message)
message = upstream_error['err_msg']
except Exception:
pass
return message


def _glob(path, pattern):
pattern = os.path.join(path, pattern)
if os.path.isdir(pattern):
Expand Down

0 comments on commit b86fe1f

Please sign in to comment.