Simplify error handling in subcommand execution
Instead of using a nested try (which repo is plagued with), use a single
try when executing the appropriate subcommand.
Change-Id: I32dbfc010c740c0cc42ef8fb6a83dfe87f87e54a
diff --git a/main.py b/main.py
index 87e9c34..35622ce 100755
--- a/main.py
+++ b/main.py
@@ -131,32 +131,31 @@
if use_pager:
RunPager(config)
+ start = time.time()
try:
- start = time.time()
- try:
- result = cmd.Execute(copts, cargs)
- finally:
- elapsed = time.time() - start
- hours, remainder = divmod(elapsed, 3600)
- minutes, seconds = divmod(remainder, 60)
- if gopts.time:
- if hours == 0:
- print('real\t%dm%.3fs' % (minutes, seconds), file=sys.stderr)
- else:
- print('real\t%dh%dm%.3fs' % (hours, minutes, seconds),
- file=sys.stderr)
+ result = cmd.Execute(copts, cargs)
except DownloadError as e:
print('error: %s' % str(e), file=sys.stderr)
- return 1
+ result = 1
except ManifestInvalidRevisionError as e:
print('error: %s' % str(e), file=sys.stderr)
- return 1
+ result = 1
except NoSuchProjectError as e:
if e.name:
print('error: project %s not found' % e.name, file=sys.stderr)
else:
print('error: no project in current directory', file=sys.stderr)
- return 1
+ result = 1
+ finally:
+ elapsed = time.time() - start
+ hours, remainder = divmod(elapsed, 3600)
+ minutes, seconds = divmod(remainder, 60)
+ if gopts.time:
+ if hours == 0:
+ print('real\t%dm%.3fs' % (minutes, seconds), file=sys.stderr)
+ else:
+ print('real\t%dh%dm%.3fs' % (hours, minutes, seconds),
+ file=sys.stderr)
return result