From a19132d3bfe2e02456b4738fb16d4aa813fe6211 Mon Sep 17 00:00:00 2001 From: Marios Balamatsias Date: Mon, 7 Dec 2020 15:58:15 +0200 Subject: [PATCH] errors: Add expected errors for device deactivation Change-type: patch Signed-off-by: Marios Balamatsias --- lib/commands/device/deactivate.ts | 7 ++++--- lib/errors.ts | 5 +++++ lib/utils/patterns.ts | 10 ++++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/commands/device/deactivate.ts b/lib/commands/device/deactivate.ts index 9fb66600..beca1415 100644 --- a/lib/commands/device/deactivate.ts +++ b/lib/commands/device/deactivate.ts @@ -71,15 +71,16 @@ export default class DeviceDeactivateCmd extends Command { const uuid = params.uuid; const deactivationWarning = ` - Warning! Deactivating a device will charge a fee equivalent to the normal monthly cost for the device (e.g. $1 for an essentials device); the device will not be charged again until it comes online. +`; -Are you sure you want to deactivate device ${uuid} ?`; + const warning = `Are you sure you want to deactivate device ${uuid} ?`; + console.error(deactivationWarning); // Confirm - await patterns.confirm(options.yes, deactivationWarning); + await patterns.confirm(options.yes, warning); // Deactivate await balena.models.device.deactivate(uuid); } diff --git a/lib/errors.ts b/lib/errors.ts index 5a8696b0..d2dd8130 100644 --- a/lib/errors.ts +++ b/lib/errors.ts @@ -142,6 +142,9 @@ const messages: { Try logging in again with the "balena login" command.`, }; +// TODO remove these regexes when we have a way of uniquely indentifying errors. +// related issue https://github.com/balena-io/balena-sdk/issues/1025 +// related issue https://github.com/balena-io/balena-cli/issues/2126 const EXPECTED_ERROR_REGEXES = [ /^BalenaAmbiguousApplication/, // balena-sdk /^BalenaAmbiguousDevice/, // balena-sdk @@ -149,6 +152,8 @@ const EXPECTED_ERROR_REGEXES = [ /^BalenaDeviceNotFound/, // balena-sdk /^BalenaExpiredToken/, // balena-sdk /^BalenaInvalidDeviceType/, // balena-sdk + /Cannot deactivate devices/i, // balena-api + /Devices must be offline in order to be deactivated\.$/i, // balena-api /Request error: Unauthorized$/, // balena-sdk /^Missing \d+ required arg/, // oclif parser: RequiredArgsError /Missing required flag/, // oclif parser: RequiredFlagError diff --git a/lib/utils/patterns.ts b/lib/utils/patterns.ts index 7ca54cdc..8eb67f64 100644 --- a/lib/utils/patterns.ts +++ b/lib/utils/patterns.ts @@ -25,6 +25,7 @@ import { import { getBalenaSdk, getVisuals, stripIndent, getCliForm } from './lazy'; import validation = require('./validation'); import { delay } from './helpers'; +import { isV13 } from './version'; export function authenticate(options: {}): Promise { const balena = getBalenaSdk(); @@ -147,7 +148,11 @@ export async function confirm( ) { if (yesOption) { if (yesMessage) { - console.log(yesMessage); + if (isV13()) { + console.error(yesMessage); + } else { + console.log(yesMessage); + } } return; } @@ -159,7 +164,8 @@ export async function confirm( }); if (!confirmed) { - const err = new Error('Aborted'); + const err = new ExpectedError('Aborted'); + // TODO remove this deprecated function (exitWithExpectedError) if (exitIfDeclined) { exitWithExpectedError(err); }