Skip to content

Commit

Permalink
fix: add new ValidationError so that we show them in red in the output
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysh committed Oct 8, 2021
1 parent 398951e commit 8c8c8b9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/cli/commands/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
FailedToRunTestError,
MonitorError,
MissingArgError,
ValidationError,
} from '../../../lib/errors';
import { isMultiProjectScan } from '../../../lib/is-multi-project-scan';
import { getEcosystem, monitorEcosystem } from '../../../lib/ecosystems';
Expand Down Expand Up @@ -339,15 +340,15 @@ function getProjectAttribute<T>(
// When it's specified without the =, we raise an explicit error to avoid
// accidentally clearing the existing values.
if (options[attribute] === true) {
throw new Error(
throw new ValidationError(
`--${attribute} must contain an '=' with a comma-separated list of values. To clear all existing values, pass no values i.e. --${attribute}=`,
);
}

const values = options[attribute].split(',');
const extra = values.filter((value) => !permittedValues.includes(value));
if (extra.length > 0) {
throw new Error(
throw new ValidationError(
`${extra.length} invalid ${attribute}: ${extra.join(', ')}. ` +
`Possible values are: ${permittedValues.join(', ')}`,
);
Expand Down Expand Up @@ -394,7 +395,7 @@ export function generateTags(options): Tag[] | undefined {
// When it's specified without the =, we raise an explicit error to avoid
// accidentally clearing the existing tags;
if (options.tags === true) {
throw new Error(
throw new ValidationError(
`--tags must contain an '=' with a comma-separated list of pairs (also separated with an '='). To clear all existing values, pass no values i.e. --tags=`,
);
}
Expand All @@ -405,7 +406,7 @@ export function generateTags(options): Tag[] | undefined {
for (const keyEqualsValue of keyEqualsValuePairs) {
const parts = keyEqualsValue.split('=');
if (parts.length !== 2) {
throw new Error(
throw new ValidationError(
`The tag "${keyEqualsValue}" does not have an "=" separating the key and value.`,
);
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { NoSupportedManifestsFoundError } from './no-supported-manifests-found';
export { NoSupportedSastFiles } from './no-supported-sast-files-found';
export { CustomError } from './custom-error';
export { MonitorError } from './monitor-error';
export { ValidationError } from './validation-error';
export { ConnectionTimeoutError } from './connection-timeout-error';
export { FailedToLoadPolicyError } from './failed-to-load-policy-error';
export { PolicyNotFoundError } from './policy-not-found-error';
Expand Down
8 changes: 8 additions & 0 deletions src/lib/errors/validation-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { CustomError } from './custom-error';

export class ValidationError extends CustomError {
constructor(message: string) {
super(message);
this.userMessage = message;
}
}

0 comments on commit 8c8c8b9

Please sign in to comment.