Skip to content

Commit

Permalink
Fixed KeyError on dark item bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjake committed Jan 27, 2020
1 parent 86aa161 commit deb0d91
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
9 changes: 7 additions & 2 deletions internetarchive/cli/ia_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import six

from internetarchive.cli.argparser import get_args_dict, get_args_dict_many_write
from internetarchive.exceptions import ItemLocateError

# Only import backports.csv for Python2 (in support of FreeBSD port).
PY2 = sys.version_info[0] == 2
Expand All @@ -73,8 +74,12 @@
def modify_metadata(item, metadata, args):
append = True if args['--append'] else False
append_list = True if args['--append-list'] else False
r = item.modify_metadata(metadata, target=args['--target'], append=append,
priority=args['--priority'], append_list=append_list)
try:
r = item.modify_metadata(metadata, target=args['--target'], append=append,
priority=args['--priority'], append_list=append_list)
except ItemLocateError as exc:
print('{} - error: {}'.format(item.identifier, str(exc)), file=sys.stderr)
sys.exit(1)
if not r.json()['success']:
error_msg = r.json()['error']
if 'no changes' in r.content.decode('utf-8'):
Expand Down
8 changes: 8 additions & 0 deletions internetarchive/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@

class AuthenticationError(Exception):
"""Authentication Failed"""

class ItemLocateError(Exception):
def __init__(self, *args, **kwargs):
default_message = 'Item cannot be located because it is dark or does not exist.'
if args or kwargs:
super().__init__(*args, **kwargs)
else:
super().__init__(default_message)
19 changes: 13 additions & 6 deletions internetarchive/iarequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

from internetarchive import auth, __version__
from internetarchive.utils import needs_quote, delete_items_from_dict
from internetarchive.exceptions import ItemLocateError


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -252,10 +253,13 @@ def prepare_body(self, metadata, source_metadata, target, priority, append,
metadata = {target: metadata}
for key in metadata:
if key == 'metadata':
patch = prepare_patch(metadata[key],
source_metadata['metadata'],
append,
append_list)
try:
patch = prepare_patch(metadata[key],
source_metadata['metadata'],
append,
append_list)
except KeyError:
raise ItemLocateError
elif key.startswith('files'):
patch = prepare_files_patch(metadata[key],
source_metadata['files'],
Expand All @@ -276,8 +280,11 @@ def prepare_body(self, metadata, source_metadata, target, priority, append,
else:
if not target or 'metadata' in target:
target = 'metadata'
patch = prepare_patch(metadata, source_metadata['metadata'], append,
append_list)
try:
patch = prepare_patch(metadata, source_metadata['metadata'], append,
append_list)
except KeyError:
raise ItemLocateError
elif 'files' in target:
patch = prepare_files_patch(metadata, source_metadata['files'], append,
target, append_list)
Expand Down

0 comments on commit deb0d91

Please sign in to comment.