diff --git a/src/cmds/orgs:data.ts b/src/cmds/orgs:data.ts index 911fc7e0..40763c75 100644 --- a/src/cmds/orgs:data.ts +++ b/src/cmds/orgs:data.ts @@ -1,4 +1,5 @@ import * as debugLib from 'debug'; +import * as yargs from 'yargs'; const debug = debugLib('snyk:orgs-data-script'); import { getLoggingPath } from '../lib/get-logging-path'; @@ -75,9 +76,9 @@ export async function handler(argv: { console.log(orgsMessage); } catch (e) { + const errorMessage = `ERROR! Failed to generate data. Try running with \`DEBUG=snyk* for more info\`.\nERROR: ${e.message}`; debug('Failed to generate data.\n' + e); - console.error( - `ERROR! Failed to generate data. Try running with \`DEBUG=snyk* for more info\`.\nERROR: ${e}`, - ); + console.error(errorMessage); + yargs.exit(1, new Error(errorMessage)); } } diff --git a/test/system/orgs:data/errors.test.ts b/test/system/orgs:data/errors.test.ts index 1bb10860..6572a5fe 100644 --- a/test/system/orgs:data/errors.test.ts +++ b/test/system/orgs:data/errors.test.ts @@ -17,4 +17,25 @@ describe('General `snyk-api-import orgs:data <...>`', () => { done(); }); }); + it('Shows error when token is invalid', (done) => { + const groupId = 'hello'; + exec( + `node ${main} orgs:data --source=github --groupId=${groupId}`, + { + env: { + PATH: process.env.PATH, + GITHUB_TOKEN: 'invalid', + SNYK_LOG_PATH: __dirname, + }, + }, + (err, stdout, stderr) => { + expect(err!.message).toMatch('ERROR: Bad credentials'); + expect(stdout).toEqual(''); + expect(stderr).toMatch('ERROR: Bad credentials'); + }, + ).on('exit', (code) => { + expect(code).toEqual(1); + done(); + }); + }); });