Skip to content

Commit

Permalink
fix(devices): fixes error handling when deleting a device
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Aug 3, 2023
1 parent 288c773 commit bea4695
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion riocli/device/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
import click
from click_help_colors import HelpColorsCommand
from requests import Response

from riocli.config import new_client
from riocli.constants import Colors, Symbols
Expand Down Expand Up @@ -42,10 +43,27 @@ def delete_device(device_name: str, device_guid: str, force: bool, spinner=None)

try:
client = new_client(with_project=True)
client.delete_device(device_id=device_guid)
handle_device_delete_error(client.delete_device(device_id=device_guid))
spinner.text = click.style('Device deleted successfully', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
spinner.text = click.style('Failed to delete device: {}'.format(e), fg=Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1) from e


def handle_device_delete_error(response: Response):
if response.status_code < 400:
return

data = response.json()

if data.get('status') != 'error':
return

error = data.get('response', {}).get('error')
if 'deployments' in error:
msg = 'Device has running deployments. Please de-provision them before deleting the device.'
raise Exception(msg)

raise Exception(error)

0 comments on commit bea4695

Please sign in to comment.