Skip to content

Commit

Permalink
Merge remote-tracking branch 'Grabauskas/improved-err-output'
Browse files Browse the repository at this point in the history
  • Loading branch information
willem-delbare committed Apr 10, 2024
2 parents 5b6afde + 52fbece commit cc5e4c9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 61 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
42 changes: 18 additions & 24 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/apiKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
61 changes: 28 additions & 33 deletions src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
};

Expand Down

0 comments on commit cc5e4c9

Please sign in to comment.