Error handling: add ExpectedError type as alternative to exitWithExpectedError()

Change-type: patch
Signed-off-by: Paulo Castro <paulo@balena.io>
This commit is contained in:
Paulo Castro
2019-10-31 01:40:57 +00:00
parent 268bc36843
commit a25a52c21b
3 changed files with 43 additions and 18 deletions

View File

@ -22,6 +22,7 @@ import _ = require('lodash');
import _form = require('resin-cli-form');
import _visuals = require('resin-cli-visuals');
import { NotLoggedInError } from '../errors';
import messages = require('./messages');
import validation = require('./validation');
@ -77,16 +78,28 @@ export function authenticate(options: {}): Bluebird<void> {
});
}
export async function exitIfNotLoggedIn(): Promise<void> {
export async function checkLoggedIn(): Promise<void> {
const balena = getBalenaSdk();
if (!(await balena.auth.isLoggedIn())) {
exitWithExpectedError(stripIndent`
throw new NotLoggedInError(stripIndent`
You have to log in to continue
Run the following command to go through the login wizard:
$ balena login`);
}
}
export async function exitIfNotLoggedIn(): Promise<void> {
try {
await checkLoggedIn();
} catch (error) {
if (error instanceof NotLoggedInError) {
exitWithExpectedError(error);
} else {
throw error;
}
}
}
export function askLoginType() {
return getForm().ask({
message: 'How would you like to login?',