diff --git a/lib/index.js b/lib/index.js index affef9c..ddb156b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,7 +10,7 @@ export const program = new Command(); program .name('Aikido API Client') .description('CLI api client to easily integrate the Aikido public CI API into custom deploy scripts') - .version('1.0.3'); + .version('1.0.4'); apiKey.cliSetup(program); scan.cliSetup(program); upload.cliSetup(program); diff --git a/lib/output.js b/lib/output.js index 76eaaf4..aa3cd77 100644 --- a/lib/output.js +++ b/lib/output.js @@ -17,30 +17,24 @@ export const outputError = (message, exitCode = 1) => { }); }; export const outputHttpError = (axiosError) => { - if (axiosError.code === 'ECONNREFUSED') { - outputError(`Could not connect to Aikido API (${axiosError.message}). Please verify your network settings`); - } - else if (axiosError.response?.status && - axiosError.response?.status === 401) { - outputError('Request failed. The provided api key is most likely no longer valid and has been rotated or revoked. Visit https://app.aikido.dev/settings/integrations/continuous-integration to generate a new key.'); - } - else if (axiosError.response?.status && - axiosError.response?.status === 403) { - outputError('Could not authenticate with the Aikido API. Please verify your Aikdio API key.'); - } - else if (axiosError.response?.status && - axiosError.response?.status === 500) { - outputError('Something went wrong contacting the Aikido API.'); - } - const statusStr = axiosError.response?.status - ? ` (${axiosError.response?.status})` - : ''; - if (axiosError.response) { - outputDebug(axiosError.response?.status); - outputDebug(axiosError.response?.headers); - outputDebug(axiosError.response?.data); - } - outputError(`Something unexpected went wrong${statusStr}... Please contact us if this problem persists.`); + outputDebug(axiosError); + if (axiosError.isAxiosError) { + if (axiosError.response) { + const statusStr = `${axiosError.response.status} ${axiosError.response.statusText}`; + switch (axiosError.response.status) { + case 401: + return outputError(`${statusStr}: The provided api key is most likely no longer valid and has been rotated or revoked. Visit https://app.aikido.dev/settings/integrations/continuous-integration to generate a new key.`); + case 403: + return outputError(`${statusStr}: Could not authenticate with the Aikido API. Please verify your Aikdio API key.`); + case 500: + return outputError(`${statusStr}: Something went wrong contacting the Aikido API. Please try again later.`); + default: + return outputError(`${statusStr}: ${axiosError.response.data ?? ''} Please contact us if this problem persists.`); + } + } + return outputError(`${axiosError.name}: ${axiosError.message}`); + } + return outputError('Unexpected error occurred. Please contact us if this problem persists.'); }; export const startSpinner = (message) => { if (process.env.QUIET) { diff --git a/package.json b/package.json index da2703b..0599e6a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@aikidosec/ci-api-client", - "version": "1.0.3", + "version": "1.0.4", "description": "CLI api client to easily integrate the Aikido public CI API into custom deploy scripts", "license": "MIT", "author": "Bert Devriese ", diff --git a/src/commands/apiKey.ts b/src/commands/apiKey.ts index ff8dc63..f1e6d29 100644 --- a/src/commands/apiKey.ts +++ b/src/commands/apiKey.ts @@ -5,7 +5,8 @@ import { outputError, outputLog } from '../output.js'; const API_KEY_REGEX = /^AIK_CI_[a-zA-Z0-9]{64}$/g; function cli(apiKey: string): void { - const isValidApiKey = API_KEY_REGEX.test(apiKey) || apiKey.trim().length === 128; + const isValidApiKey = + API_KEY_REGEX.test(apiKey) || apiKey.trim().length === 128; if (apiKey && !isValidApiKey) { outputError('That does not seem right, please verify your api key'); } diff --git a/src/index.ts b/src/index.ts index 97ec989..780cc9c 100755 --- a/src/index.ts +++ b/src/index.ts @@ -19,7 +19,7 @@ program .description( 'CLI api client to easily integrate the Aikido public CI API into custom deploy scripts' ) - .version('1.0.3'); + .version('1.0.4'); // Load in all app commands and set them up in the `program` instance apiKey.cliSetup(program); diff --git a/src/output.ts b/src/output.ts index a0e0eab..1a7fabe 100644 --- a/src/output.ts +++ b/src/output.ts @@ -30,43 +30,38 @@ export const outputError = (message: string, exitCode: number = 1): void => { }; export const outputHttpError = (axiosError: AxiosError): void => { - if (axiosError.code === 'ECONNREFUSED') { - outputError( - `Could not connect to Aikido API (${axiosError.message}). Please verify your network settings` - ); - } else if ( - axiosError.response?.status && - axiosError.response?.status === 401 - ) { - outputError( - 'Request failed. The provided api key is most likely no longer valid and has been rotated or revoked. Visit https://app.aikido.dev/settings/integrations/continuous-integration to generate a new key.' - ); - } else if ( - axiosError.response?.status && - axiosError.response?.status === 403 - ) { - outputError( - 'Could not authenticate with the Aikido API. Please verify your Aikdio API key.' - ); - } else if ( - axiosError.response?.status && - axiosError.response?.status === 500 - ) { - outputError('Something went wrong contacting the Aikido API.'); - } + outputDebug(axiosError); - const statusStr = axiosError.response?.status - ? ` (${axiosError.response?.status})` - : ''; + if (axiosError.isAxiosError) { + if (axiosError.response) { + const statusStr = `${axiosError.response.status} ${axiosError.response.statusText}`; + switch (axiosError.response.status) { + case 401: + return outputError( + `${statusStr}: The provided api key is most likely no longer valid and has been rotated or revoked. Visit https://app.aikido.dev/settings/integrations/continuous-integration to generate a new key.` + ); + case 403: + return outputError( + `${statusStr}: Could not authenticate with the Aikido API. Please verify your Aikdio API key.` + ); + case 500: + return outputError( + `${statusStr}: Something went wrong contacting the Aikido API. Please try again later.` + ); + default: + return outputError( + `${statusStr}: ${ + axiosError.response.data ?? '' + } Please contact us if this problem persists.` + ); + } + } - if (axiosError.response) { - outputDebug(axiosError.response?.status); - outputDebug(axiosError.response?.headers); - outputDebug(axiosError.response?.data); + return outputError(`${axiosError.name}: ${axiosError.message}`); } - outputError( - `Something unexpected went wrong${statusStr}... Please contact us if this problem persists.` + return outputError( + 'Unexpected error occurred. Please contact us if this problem persists.' ); };