From 1a7fb56773881a5a929078f3b3f1388c9b1c3bce Mon Sep 17 00:00:00 2001 From: Giedrius Grabauskas Date: Tue, 26 Mar 2024 09:32:08 +0200 Subject: [PATCH 1/6] Improved error output --- lib/output.js | 51 +++++++++++++++++--------------- src/output.ts | 80 +++++++++++++++++++++++++++++---------------------- 2 files changed, 73 insertions(+), 58 deletions(-) diff --git a/lib/output.js b/lib/output.js index 76eaaf4..2acfe72 100644 --- a/lib/output.js +++ b/lib/output.js @@ -17,30 +17,35 @@ 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`); + if (axiosError.isAxiosError) { + if (axiosError.code === 'ECONNREFUSED') { + outputError(`Could not connect to Aikido API (${axiosError.message}). Please verify your network settings.`); + } + else if (axiosError.response) { + outputDebug(axiosError.response.status); + outputDebug(axiosError.response.headers); + outputDebug(axiosError.response.data); + 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.`); + } + } + else if (axiosError.request) { + return outputError('No response received from the server. Please try again later.'); + } + else if (axiosError.code === 'ECONNREFUSED') { + return outputError(`Could not connect to Aikido API (${axiosError.message}). Please verify your network settings.`); + } + outputDebug(axiosError); + return outputError(`Error: (${axiosError.message}). Please contact us if this problem persists.`); } - 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.`); }; export const startSpinner = (message) => { if (process.env.QUIET) { diff --git a/src/output.ts b/src/output.ts index a0e0eab..dacc610 100644 --- a/src/output.ts +++ b/src/output.ts @@ -30,44 +30,54 @@ 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.'); - } + if (axiosError.isAxiosError) { + if (axiosError.code === 'ECONNREFUSED') { + outputError( + `Could not connect to Aikido API (${axiosError.message}). Please verify your network settings.` + ); + } else if (axiosError.response) { + // The request was made and the server responded with a status code + outputDebug(axiosError.response.status); + outputDebug(axiosError.response.headers); + outputDebug(axiosError.response.data); - const statusStr = axiosError.response?.status - ? ` (${axiosError.response?.status})` - : ''; + 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.` + ); + } + // The request was made but no response was received + } else if (axiosError.request) { + return outputError( + 'No response received from the server. Please try again later.' + ); + } else if (axiosError.code === 'ECONNREFUSED') { + return outputError( + `Could not connect to Aikido API (${axiosError.message}). Please verify your network settings.` + ); + } - if (axiosError.response) { - outputDebug(axiosError.response?.status); - outputDebug(axiosError.response?.headers); - outputDebug(axiosError.response?.data); + outputDebug(axiosError); + return outputError( + `Error: (${axiosError.message}). Please contact us if this problem persists.` + ); } - - outputError( - `Something unexpected went wrong${statusStr}... Please contact us if this problem persists.` - ); }; // Start a new spinner From 015ec12abcc05a187346b62e46f5ea39444136fb Mon Sep 17 00:00:00 2001 From: Giedrius Grabauskas Date: Tue, 26 Mar 2024 09:35:27 +0200 Subject: [PATCH 2/6] Formatted code --- src/commands/apiKey.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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'); } From d68cda48432f0431be929066aed097a75ecf30ba Mon Sep 17 00:00:00 2001 From: Giedrius Grabauskas Date: Tue, 26 Mar 2024 09:36:39 +0200 Subject: [PATCH 3/6] Version bump to 1.0.4 --- lib/index.js | 2 +- package.json | 2 +- src/index.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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/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/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); From 25a2199be442945effec30273c7441dfbb72881d Mon Sep 17 00:00:00 2001 From: Giedrius Grabauskas Date: Tue, 26 Mar 2024 09:58:35 +0200 Subject: [PATCH 4/6] Improving errors --- lib/output.js | 12 +++++------- src/output.ts | 17 +++++++++-------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/output.js b/lib/output.js index 2acfe72..69968cc 100644 --- a/lib/output.js +++ b/lib/output.js @@ -17,9 +17,10 @@ export const outputError = (message, exitCode = 1) => { }); }; export const outputHttpError = (axiosError) => { + outputDebug(axiosError); if (axiosError.isAxiosError) { if (axiosError.code === 'ECONNREFUSED') { - outputError(`Could not connect to Aikido API (${axiosError.message}). Please verify your network settings.`); + return outputError(`Could not connect to Aikido API (${axiosError.message}). Please verify your network settings.`); } else if (axiosError.response) { outputDebug(axiosError.response.status); @@ -38,14 +39,11 @@ export const outputHttpError = (axiosError) => { } } else if (axiosError.request) { - return outputError('No response received from the server. Please try again later.'); - } - else if (axiosError.code === 'ECONNREFUSED') { - return outputError(`Could not connect to Aikido API (${axiosError.message}). Please verify your network settings.`); + return outputError(`Error: ${axiosError.message}. No response received from the server, please try again later.`); } - outputDebug(axiosError); - return outputError(`Error: (${axiosError.message}). Please contact us if this problem persists.`); + return outputError(`Error: ${axiosError.message}. Please contact us if this problem persists.`); } + return outputError('Unexpected error occurred. Please contact us if this problem persists.'); }; export const startSpinner = (message) => { if (process.env.QUIET) { diff --git a/src/output.ts b/src/output.ts index dacc610..346491f 100644 --- a/src/output.ts +++ b/src/output.ts @@ -30,9 +30,11 @@ export const outputError = (message: string, exitCode: number = 1): void => { }; export const outputHttpError = (axiosError: AxiosError): void => { + outputDebug(axiosError); + if (axiosError.isAxiosError) { if (axiosError.code === 'ECONNREFUSED') { - outputError( + return outputError( `Could not connect to Aikido API (${axiosError.message}). Please verify your network settings.` ); } else if (axiosError.response) { @@ -65,19 +67,18 @@ export const outputHttpError = (axiosError: AxiosError): void => { // The request was made but no response was received } else if (axiosError.request) { return outputError( - 'No response received from the server. Please try again later.' - ); - } else if (axiosError.code === 'ECONNREFUSED') { - return outputError( - `Could not connect to Aikido API (${axiosError.message}). Please verify your network settings.` + `Error: ${axiosError.message}. No response received from the server, please try again later.` ); } - outputDebug(axiosError); return outputError( - `Error: (${axiosError.message}). Please contact us if this problem persists.` + `Error: ${axiosError.message}. Please contact us if this problem persists.` ); } + + return outputError( + 'Unexpected error occurred. Please contact us if this problem persists.' + ); }; // Start a new spinner From 04470a139b1f338d16e7dab8e2b815d801a8c119 Mon Sep 17 00:00:00 2001 From: Giedrius Grabauskas Date: Tue, 26 Mar 2024 10:08:21 +0200 Subject: [PATCH 5/6] Improved output --- lib/output.js | 14 ++++---------- src/output.ts | 24 +++++++----------------- 2 files changed, 11 insertions(+), 27 deletions(-) diff --git a/lib/output.js b/lib/output.js index 69968cc..73cf2ea 100644 --- a/lib/output.js +++ b/lib/output.js @@ -19,13 +19,7 @@ export const outputError = (message, exitCode = 1) => { export const outputHttpError = (axiosError) => { outputDebug(axiosError); if (axiosError.isAxiosError) { - if (axiosError.code === 'ECONNREFUSED') { - return outputError(`Could not connect to Aikido API (${axiosError.message}). Please verify your network settings.`); - } - else if (axiosError.response) { - outputDebug(axiosError.response.status); - outputDebug(axiosError.response.headers); - outputDebug(axiosError.response.data); + if (axiosError.response) { const statusStr = `${axiosError.response.status} ${axiosError.response.statusText}`; switch (axiosError.response.status) { case 401: @@ -38,10 +32,10 @@ export const outputHttpError = (axiosError) => { return outputError(`${statusStr}: ${axiosError.response.data ?? ''} Please contact us if this problem persists.`); } } - else if (axiosError.request) { - return outputError(`Error: ${axiosError.message}. No response received from the server, please try again later.`); + if (axiosError.request) { + outputError(`No response received from the server. Please verify your network settings and try again.`); } - return outputError(`Error: ${axiosError.message}. 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.'); }; diff --git a/src/output.ts b/src/output.ts index 346491f..c5b8d91 100644 --- a/src/output.ts +++ b/src/output.ts @@ -33,16 +33,7 @@ export const outputHttpError = (axiosError: AxiosError): void => { outputDebug(axiosError); if (axiosError.isAxiosError) { - if (axiosError.code === 'ECONNREFUSED') { - return outputError( - `Could not connect to Aikido API (${axiosError.message}). Please verify your network settings.` - ); - } else if (axiosError.response) { - // The request was made and the server responded with a status code - outputDebug(axiosError.response.status); - outputDebug(axiosError.response.headers); - outputDebug(axiosError.response.data); - + if (axiosError.response) { const statusStr = `${axiosError.response.status} ${axiosError.response.statusText}`; switch (axiosError.response.status) { case 401: @@ -64,16 +55,15 @@ export const outputHttpError = (axiosError: AxiosError): void => { } Please contact us if this problem persists.` ); } - // The request was made but no response was received - } else if (axiosError.request) { - return outputError( - `Error: ${axiosError.message}. No response received from the server, please try again later.` + } + + if (axiosError.request) { + outputError( + `No response received from the server. Please verify your network settings and try again.` ); } - return outputError( - `Error: ${axiosError.message}. Please contact us if this problem persists.` - ); + return outputError(`${axiosError.name}: ${axiosError.message}`); } return outputError( From 52fbecea70d0ec4e124e1b32086e4b6eedb82c83 Mon Sep 17 00:00:00 2001 From: Giedrius Grabauskas Date: Tue, 26 Mar 2024 10:11:33 +0200 Subject: [PATCH 6/6] Improved error --- lib/output.js | 3 --- src/output.ts | 6 ------ 2 files changed, 9 deletions(-) diff --git a/lib/output.js b/lib/output.js index 73cf2ea..aa3cd77 100644 --- a/lib/output.js +++ b/lib/output.js @@ -32,9 +32,6 @@ export const outputHttpError = (axiosError) => { return outputError(`${statusStr}: ${axiosError.response.data ?? ''} Please contact us if this problem persists.`); } } - if (axiosError.request) { - outputError(`No response received from the server. Please verify your network settings and try again.`); - } return outputError(`${axiosError.name}: ${axiosError.message}`); } return outputError('Unexpected error occurred. Please contact us if this problem persists.'); diff --git a/src/output.ts b/src/output.ts index c5b8d91..1a7fabe 100644 --- a/src/output.ts +++ b/src/output.ts @@ -57,12 +57,6 @@ export const outputHttpError = (axiosError: AxiosError): void => { } } - if (axiosError.request) { - outputError( - `No response received from the server. Please verify your network settings and try again.` - ); - } - return outputError(`${axiosError.name}: ${axiosError.message}`); }