Skip to content

Commit

Permalink
Merge pull request #615 from snyk/feat/refactor-errors
Browse files Browse the repository at this point in the history
Feat: refactor policy errors
  • Loading branch information
lili2311 committed Jul 1, 2019
2 parents d72486a + 4ada94b commit 7821f01
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/cli/commands/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ module.exports = displayPolicy;

const policy = require('snyk-policy');
const display = require('../../lib/display-policy');
const errors = require('../../lib/errors');

function displayPolicy(path) {
return policy.load(path || process.cwd())
.then(display)
.catch((e) => {
let error;
if (e.code === 'ENOENT') {
e.code = 'MISSING_DOTFILE';
error = new errors.PolicyNotFoundError();
} else {
error = new errors.FailedToLoadPolicyError();
error.innerError = e;
}
throw e;
throw error;
});
}
12 changes: 8 additions & 4 deletions src/cli/commands/protect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as detect from '../../../lib/detect';
import * as pm from '../../../lib/package-managers';
import { CustomError } from '../../../lib/errors';
import { LegacyVulnApiResult } from '../../../lib/snyk-test/legacy';
import * as errors from '../../../lib/errors';

const debug = debugModule('snyk');

Expand Down Expand Up @@ -54,11 +55,14 @@ async function protectFunc(options: types.ProtectOptions & types.Options & types
return patch(protectOptions);
}
return 'Nothing to do';
} catch (error) {
if (error.code === 'ENOENT') {
error.code = 'MISSING_DOTFILE';
} catch (e) {
let error;
if (e.code === 'ENOENT') {
error = new errors.PolicyNotFoundError();
} else {
error = new errors.FailedToLoadPolicyError();
error.innerError = e;
}

throw error;
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/lib/errors/failed-to-load-policy-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {CustomError} from './custom-error';

export class FailedToLoadPolicyError extends CustomError {
private static ERROR_CODE: number = 422;
private static ERROR_STRING_CODE: string = 'POLICY_LOAD_FAILED';
private static ERROR_MESSAGE: string =
'Could not load policy file.';

constructor() {
super(FailedToLoadPolicyError.ERROR_MESSAGE);
this.code = FailedToLoadPolicyError.ERROR_CODE;
this.strCode = FailedToLoadPolicyError.ERROR_STRING_CODE;
this.userMessage = FailedToLoadPolicyError.ERROR_MESSAGE;
}
}
2 changes: 2 additions & 0 deletions src/lib/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export {NoSupportedManifestsFoundError} from './no-supported-manifests-found';
export {CustomError} from './custom-error';
export {MonitorError} from './monitor-error';
export {ConnectionTimeoutError} from './connection-timeout-error';
export {FailedToLoadPolicyError} from './failed-to-load-policy-error';
export {PolicyNotFoundError} from './policy-not-found-error';
2 changes: 0 additions & 2 deletions src/lib/errors/legacy-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const errors = {
connect: 'Check your network connection, failed to connect to Snyk API',
endpoint: 'The Snyk API is not available on ' + config.API,
auth: 'Unauthorized: please ensure you are logged in using `snyk auth`',
dotfile: 'Try running `snyk wizard` to define a Snyk protect policy',
oldsnyk: 'You have an alpha format Snyk policy file in this directory. ' +
'Please remove it, and re-create using `snyk wizard`',
notfound: 'The package could not be found or does not exist',
Expand Down Expand Up @@ -50,7 +49,6 @@ const codes = {
401: errors.auth,
400: errors.invalidSeverityThreshold,
Unauthorized: errors.auth,
MISSING_DOTFILE: errors.dotfile,
MISSING_NODE_MODULES: errors.nodeModules,
OLD_DOTFILE_FORMAT: errors.oldsnyk,
FAIL_PATCH: errors.patchfail,
Expand Down
15 changes: 15 additions & 0 deletions src/lib/errors/policy-not-found-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {CustomError} from './custom-error';

export class PolicyNotFoundError extends CustomError {
private static ERROR_CODE: number = 404;
private static ERROR_STRING_CODE: string = 'MISSING_DOTFILE';
private static ERROR_MESSAGE: string =
'Could not load policy. Try running `snyk wizard` to define a Snyk protect policy';

constructor() {
super(PolicyNotFoundError.ERROR_MESSAGE);
this.code = PolicyNotFoundError.ERROR_CODE;
this.strCode = PolicyNotFoundError.ERROR_STRING_CODE;
this.userMessage = PolicyNotFoundError.ERROR_MESSAGE;
}
}

0 comments on commit 7821f01

Please sign in to comment.