#68 Do not throw out exceptions from cli
Merged 3 years ago by frantisekz. Opened 3 years ago by frantisekz.

file modified
+18 -14
@@ -15,7 +15,7 @@ 

  from . import config

  from . import image

  from . import instance

- from .exceptions import TestcloudCliError, TestcloudPermissionsError

+ from .exceptions import TestcloudPermissionsError, TestcloudInstanceError

  

  config_data = config.get_config()

  
@@ -79,12 +79,11 @@ 

  

      # can't create existing instances

      if existing_instance is not None:

-         raise TestcloudCliError("A testcloud instance named {} already "

-                                 "exists at {}. Use 'testcloud instance start "

-                                 "{}' to start the instance or remove it before"

-                                 " re-creating ".format(args.name,

-                                                        existing_instance.path,

-                                                        args.name))

+         log.error("A testcloud instance named {} already exists at {}. Use 'testcloud instance start "

+                 "{}' to start the instance or remove it before re-creating.".format(

+                     args.name,existing_instance.path, args.name)

+              )

+         sys.exit(1)

  

      else:

          tc_instance = instance.Instance(args.name, image=tc_image, connection=args.connection)
@@ -123,8 +122,8 @@ 

      tc_instance = instance.find_instance(args.name, connection=args.connection)

  

      if tc_instance is None:

-         raise TestcloudCliError("Cannot start instance {} because it does "

-                                 "not exist".format(args.name))

+         log.error("Cannot start instance {} because it does not exist".format(args.name))

+         sys.exit(1)

  

      tc_instance.start(args.timeout)

      with open(os.path.join(config_data.DATA_DIR, 'instances', args.name, 'ip'), 'r') as ip_file:
@@ -143,8 +142,8 @@ 

      tc_instance = instance.find_instance(args.name, connection=args.connection)

  

      if tc_instance is None:

-         raise TestcloudCliError("Cannot stop instance {} because it does "

-                                 "not exist".format(args.name))

+         log.error("Cannot stop instance {} because it does not exist".format(args.name))

+         sys.exit(1)

  

      tc_instance.stop()

  
@@ -160,10 +159,15 @@ 

      tc_instance = instance.find_instance(args.name, connection=args.connection)

  

      if tc_instance is None:

-         raise TestcloudCliError("Cannot remove instance {} because it does "

-                                 "not exist".format(args.name))

+         log.error("Cannot remove instance {} because it does not exist".format(args.name))

+         sys.exit(1)

+ 

+     try:

+         tc_instance.remove(autostop=args.force)

+     except TestcloudInstanceError as e:

+         log.error(e)

+         sys.exit(1)

  

-     tc_instance.remove(autostop=args.force)

  

  def _clean_instances(args):

      """Handler for 'instance clean' command. Expects the following elements in args:

file modified
-6
@@ -12,12 +12,6 @@ 

      """Common ancestor for all Testcloud exceptions"""

      pass

  

- 

- class TestcloudCliError(TestcloudException):

-     """Exception for errors having to do with Testcloud CLI processing"""

-     pass

- 

- 

  class TestcloudImageError(TestcloudException):

      """Exception for errors having to do with images and image fetching"""

      pass

Pull-Request has been merged by frantisekz

3 years ago