Don't call sys.exit from inside the class

Instead, we raise exceptions that can be caught and
handled by calling code instead
pull/496/head
yuvipanda 2018-12-12 10:42:52 -08:00
rodzic a0ad3f92e3
commit 8f56061e69
3 zmienionych plików z 19 dodań i 12 usunięć

Wyświetl plik

@ -1,6 +1,7 @@
import argparse import argparse
import sys import sys
import os import os
import docker
from .app import Repo2Docker from .app import Repo2Docker
from . import __version__ from . import __version__
from .utils import validate_and_generate_port_mapping from .utils import validate_and_generate_port_mapping
@ -314,8 +315,18 @@ def make_r2d(argv=None):
def main(): def main():
r2d = make_r2d() r2d = make_r2d()
r2d.initialize() r2d.initialize()
try:
r2d.start() r2d.start()
except docker.errors.BuildError as e:
# This is only raised by us
if r2d.debug:
r2d.log.exception(e)
sys.exit(1)
except docker.errors.ImageLoadError as e:
# This is only raised by us
if r2d.debug:
r2d.log.exception(e)
sys.exit(1)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Wyświetl plik

@ -415,7 +415,7 @@ class Repo2Docker(Application):
progress = json.loads(line.decode('utf-8')) progress = json.loads(line.decode('utf-8'))
if 'error' in progress: if 'error' in progress:
self.log.error(progress['error'], extra=dict(phase='failed')) self.log.error(progress['error'], extra=dict(phase='failed'))
sys.exit(1) raise docker.errors.ImageLoadError(progress['error'])
if 'id' not in progress: if 'id' not in progress:
continue continue
if 'progressDetail' in progress and progress['progressDetail']: if 'progressDetail' in progress and progress['progressDetail']:
@ -545,12 +545,8 @@ class Repo2Docker(Application):
api_client = docker.APIClient(version='auto', api_client = docker.APIClient(version='auto',
**kwargs_from_env()) **kwargs_from_env())
except DockerException as e: except DockerException as e:
print("Docker client initialization error. Check if docker is" self.log.exception(e)
" running on the host.") raise
print(e)
if self.log_level == logging.DEBUG:
raise e
sys.exit(1)
# If the source to be executed is a directory, continue using the # If the source to be executed is a directory, continue using the
# directory. In the case of a local directory, it is used as both the # directory. In the case of a local directory, it is used as both the
# source and target. Reusing a local directory seems better than # source and target. Reusing a local directory seems better than
@ -572,7 +568,7 @@ class Repo2Docker(Application):
if not os.path.isdir(checkout_path): if not os.path.isdir(checkout_path):
self.log.error('Subdirectory %s does not exist', self.log.error('Subdirectory %s does not exist',
self.subdir, extra=dict(phase='failure')) self.subdir, extra=dict(phase='failure'))
sys.exit(1) raise FileNotFoundError(f'Could not find {checkout_path}')
with chdir(checkout_path): with chdir(checkout_path):
for BP in self.buildpacks: for BP in self.buildpacks:
@ -608,7 +604,7 @@ class Repo2Docker(Application):
extra=dict(phase='building')) extra=dict(phase='building'))
elif 'error' in l: elif 'error' in l:
self.log.info(l['error'], extra=dict(phase='failure')) self.log.info(l['error'], extra=dict(phase='failure'))
sys.exit(1) raise docker.errors.BuildError(l['error'])
elif 'status' in l: elif 'status' in l:
self.log.info('Fetching base image...\r', self.log.info('Fetching base image...\r',
extra=dict(phase='building')) extra=dict(phase='building'))

Wyświetl plik

@ -38,7 +38,7 @@ class Git(ContentProvider):
if hash is None: if hash is None:
self.log.error('Failed to check out ref %s', ref, self.log.error('Failed to check out ref %s', ref,
extra=dict(phase='failed')) extra=dict(phase='failed'))
sys.exit(1) raise ValueError(f'Failed to check out ref {ref}')
# If the hash is resolved above, we should be able to reset to it # If the hash is resolved above, we should be able to reset to it
for line in execute_cmd(['git', 'reset', '--hard', hash], for line in execute_cmd(['git', 'reset', '--hard', hash],
cwd=output_dir, cwd=output_dir,