Skip to content

Commit

Permalink
Better handle of exceptions and errors
Browse files Browse the repository at this point in the history
Signed-off-by: Yohanna Lisnichuk <yohanitalisnichuk@gmail.com>
  • Loading branch information
yolile committed Jan 5, 2018
1 parent 1e06445 commit 3368632
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
4 changes: 2 additions & 2 deletions cove/management/commands/base_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def handle(self, files, *args, **options):

stream = options.get('stream')
if (len(files) > 1 or os.path.isdir(files[0])) and not stream:
self.stdout.write('The multiple file option must be used with the --stream option')
self.stderr.write('The multiple file option must be used with the --stream option')
sys.exit(1)

output_dir = options.get('output_dir')
Expand All @@ -45,7 +45,7 @@ def handle(self, files, *args, **options):
if delete:
shutil.rmtree(output_dir)
else:
self.stdout.write('Directory {} already exists'.format(output_dir))
self.stderr.write('Directory {} already exists'.format(output_dir))
sys.exit(1)
os.makedirs(output_dir)

Expand Down
37 changes: 22 additions & 15 deletions cove_iati/management/commands/iati_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Command(CoveBaseCommand):
def add_arguments(self, parser):
parser.add_argument('--openag', '-a', action='store_true', help='Run ruleset checks for IATI OpenAg')
parser.add_argument('--orgids', '-i', action='store_true', help='Check IATI identifier prefixes against '
'Org-ids prefixes')
'Org-ids prefixes')
super(Command, self).add_arguments(parser)

def handle(self, files, *args, **options):
Expand All @@ -22,22 +22,29 @@ def handle(self, files, *args, **options):
openag = options.get('openag')
orgids = options.get('orgids')

try:
if stream:
for file in files:
if os.path.isdir(file):
self.stdout.write('Skipping %s directory ' % str(file))
else:
if stream:
for file in files:
if os.path.isdir(file):
self.stderr.write('Skipping %s directory ' % str(file))
else:
try:
result = iati_json_output(self.output_dir, file, openag=openag,
orgids=orgids)
self.stdout.write(str(result))
else:
result.update({'file': file})
self.stdout.write(json.dumps(result, cls=SetEncoder))
except APIException as e:
self.stdout.write(json.dumps({'file': file, 'error': e}))
except CoveInputDataError as e:
self.stdout.write(json.dumps({'file': file, 'error': 'Not well formed XML: %s'
% str(e.context['error'])}))
else:
try:
result = iati_json_output(self.output_dir, files[0], openag=openag, orgids=orgids)
with open(os.path.join(self.output_dir, "results.json"), 'w+') as result_file:
json.dump(result, result_file, indent=2, cls=SetEncoder)
except APIException as e:
self.stdout.write(str(e))
sys.exit(1)
except CoveInputDataError as e:
self.stdout.write('Not well formed XML: %s' % str(e.context['error']))
sys.exit(1)
except APIException as e:
self.stderr.write(str(e))
sys.exit(1)
except CoveInputDataError as e:
self.stderr.write('Not well formed XML: %s' % str(e.context['error']))
sys.exit(1)
26 changes: 15 additions & 11 deletions cove_ocds/management/commands/ocds_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ def handle(self, files, *args, **options):
str(list(version_choices.keys()))
))

try:
if stream:
for file in files:
if os.path.isdir(file):
self.stdout.write('Skipping %s directory ' % str(file))
else:
if stream:
for file in files:
if os.path.isdir(file):
self.stderr.write('Skipping %s directory ' % str(file))
else:
try:
result = ocds_json_output(self.output_dir, file, schema_version,
convert)
self.stdout.write(str(result))
else:
result.update({'file': file})
self.stdout.write(json.dumps(result, cls=SetEncoder))
except APIException as e:
self.stdout.write(json.dumps({'file': file, 'error': e}))
else:
try:
result = ocds_json_output(self.output_dir, files[0], schema_version, convert)
with open(os.path.join(self.output_dir, 'results.json'), 'w+') as result_file:
json.dump(result, result_file, indent=2, cls=SetEncoder)
except APIException as e:
self.stdout.write(str(e))
sys.exit(1)
except APIException as e:
self.stderr.write(str(e))
sys.exit(1)

0 comments on commit 3368632

Please sign in to comment.