GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.openstack.org — Command Errors — OpenStack Command Line Client 10.5.1.dev13 documentation Command Errors¶Handling errors in OpenStackClient commands is fairly straightforward. An exception is thrown and handled by the application-level caller. Note: There are many cases that need to be filled out here. The initial version of this document considers the general command error handling as well as the specific case of commands that make multiple REST API calls and how to handle when one or more of those calls fails. General Command Errors¶The general pattern for handling OpenStackClient command-level errors is to
raise a CommandError exception with an appropriate message. This should include
conditions arising from arguments that are not valid/allowed (that are not otherwise
enforced by External Errors¶External errors are a result of things outside OpenStackClient not being as expected. Example¶This example is taken from class CreateKeypair(command.ShowOne): """Create new public key""" ## ... def take_action(self, parsed_args): compute_client = self.app.client_manager.compute public_key = parsed_args.public_key if public_key: try: with open( os.path.expanduser(parsed_args.public_key), "rb" ) as p: public_key = p.read() except IOError as e: msg = _("Key file %s not found: %s") raise exceptions.CommandError( msg % (parsed_args.public_key, e), ) keypair = compute_client.create_keypair( name=parsed_args.name, public_key=public_key, ) ## ... REST API Errors¶Most commands make a single REST API call via the supporting client library or SDK. Errors based on HTML return codes are usually handled well by default, but in some cases more specific or user-friendly messages need to be logged. Trapping the exception and raising a CommandError exception with a useful message is the correct approach. Multiple REST API Calls¶Some CLI commands make multiple calls to library APIs and thus REST APIs.
Most of the time these are
The desired behaviour is for commands to appear to the user as idempotent
whenever possible, i.e. a partial failure in a Example 1¶This example is taken from the class SetSnapshot(command.Command): """Set snapshot properties""" ## ... def take_action(self, parsed_args): volume_client = self.app.client_manager.volume snapshot = utils.find_resource( volume_client.volume_snapshots, parsed_args.snapshot, ) kwargs = {} if parsed_args.name: kwargs['name'] = parsed_args.name if parsed_args.description: kwargs['description'] = parsed_args.description result = 0 if parsed_args.property: try: volume_client.volume_snapshots.set_metadata( snapshot.id, parsed_args.property, ) except SomeException: # Need to define the exceptions to catch here LOG.error(_("Property set failed")) result += 1 if parsed_args.state: try: volume_client.volume_snapshots.reset_state( snapshot.id, parsed_args.state, ) except SomeException: # Need to define the exceptions to catch here LOG.error(_("State set failed")) result += 1 try: volume_client.volume_snapshots.update( snapshot.id, **kwargs ) except SomeException: # Need to define the exceptions to catch here LOG.error(_("Update failed")) result += 1 # NOTE(dtroyer): We need to signal the error, and a non-zero return code, # without aborting prematurely if result > 0: raise SomeNonFatalException Example 2¶This example is taken from the class DeleteNetwork(common.NetworkDelete): """Delete network(s)""" def get_parser(self, prog_name): parser = super().get_parser(prog_name) parser.add_argument( 'network', metavar="<network>", nargs="+", help=_("Network(s) to delete (name or ID)") ) return parser def take_action_delete(self, parsed_args): client = self.app.client_manager.network obj = client.find_network(network, ignore_missing=False) client.delete_network(obj) this page last updated: 2026-03-20 18:16:54
Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |