Skip to content

Commit

Permalink
Merge branch 'fix/exit-code-1-on-error' into release/cmd-1.1.1-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
vCaisim committed Oct 2, 2024
2 parents 49879c5 + 22bf4ab commit ebda733
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
9 changes: 7 additions & 2 deletions packages/cmd/src/commands/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommanderError } from '@commander-js/extra-typings';
import { ValidationError } from '@lib';
import { error } from '@logger';
import { ValidationError, Warning } from '@lib';
import { error, warnWithNoTrace } from '@logger';
import { isAxiosError } from 'axios';
import { enableDebug } from '../debug';

Expand Down Expand Up @@ -35,6 +35,11 @@ export async function commandHandler<T extends Record<string, unknown>>(
process.exit(1);
}

if (e instanceof Warning) {
warnWithNoTrace(e.message);
process.exit(1);
}

if (isAxiosError(e)) {
error(e.message);
process.exit(1);
Expand Down
2 changes: 2 additions & 0 deletions packages/cmd/src/lib/error.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export class ValidationError extends Error {}

export class Warning extends Error {}
12 changes: 6 additions & 6 deletions packages/cmd/src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,16 @@ export const warn = (...args: unknown[]) => {
return log(chalk.bgYellow.black(' WARNING '), msg);
};
export const warnWithNoTrace = (...args: unknown[]) => {
const msg = util.format(...args);
debug('WARNING: ', msg);
const msg = util.format(...formatArgs(args));
debug('WARNING: ', util.format(...args));
return log(chalk.bgYellow.black(' WARNING '), msg);
};

export const success = (...args: unknown[]) =>
log(chalk.green.bold(util.format(...args)));

export const error = (...args: unknown[]) => {
const formattedArgs = args.map((arg) =>
arg instanceof Error ? arg.message : arg
);
const msg = util.format(...formattedArgs);
const msg = util.format(...formatArgs(args));
errors.push(msg);
debug('ERROR: ', util.format(...args));
return _error(chalk.bgRed.white(' ERROR '), msg);
Expand Down Expand Up @@ -80,6 +77,9 @@ export const blockEnd = (label = '', lineLength = 64) => {
);
};

const formatArgs = (args: unknown[]) =>
args.map((arg) => (arg instanceof Error ? arg.message : arg));

export const spacer = (n: number = 2) =>
console.log(Array(n).fill('').join('\n'));

Expand Down
3 changes: 2 additions & 1 deletion packages/cmd/src/services/cache/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { retrieveCache } from '../../api';
import { PRESETS } from '../../commands/cache/options';
import { getCacheCommandConfig } from '../../config/cache';
import { getCI } from '../../env/ciProvider';
import { Warning } from '../../lib';
import { unzipBuffer } from './fs';
import { MetaFile } from './lib';
import { download } from './network';
Expand Down Expand Up @@ -51,7 +52,7 @@ export async function handleGetCache() {
e.response?.status &&
(e.response?.status === 403 || e.response?.status === 404)
) {
throw new Error(`Cache with ID "${result.cacheId}" not found`);
throw new Warning(`Cache with ID "${result.cacheId}" not found`);
}
}

Expand Down

0 comments on commit ebda733

Please sign in to comment.