Skip to content

Commit

Permalink
fix: ensure errors from most imports are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
Jahed Ahmed committed Nov 25, 2021
1 parent d0fb7cc commit a8835ab
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/cli/exit-codes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const EXIT_CODES = {
VULNS_FOUND: 1,
ERROR: 2,
NO_SUPPORTED_PROJECTS_DETECTED: 3,
};
12 changes: 12 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { callHandlingUnexpectedErrors } from '../lib/unexpected-error';
import { EXIT_CODES } from './exit-codes';

/**
* By using a dynamic import, we can add error handlers before evaluating any
* further modules. This way, if a module has errors, it'll be caught and
* handled as we expect.
*/
callHandlingUnexpectedErrors(async () => {
const { main } = await import('./main');
await main();
}, EXIT_CODES.ERROR);
11 changes: 2 additions & 9 deletions src/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,9 @@ import {
import { SarifFileOutputEmptyError } from '../lib/errors/empty-sarif-output-error';
import { InvalidDetectionDepthValue } from '../lib/errors/invalid-detection-depth-value';
import { obfuscateArgs } from '../lib/utils';
import { callHandlingUnexpectedErrors } from '../lib/unexpected-error';
import { EXIT_CODES } from './exit-codes';

const debug = Debug('snyk');
const EXIT_CODES = {
VULNS_FOUND: 1,
ERROR: 2,
NO_SUPPORTED_PROJECTS_DETECTED: 3,
};

async function runCommand(args: Args) {
const commandResult = await args.method(...args.options._);
Expand Down Expand Up @@ -241,7 +236,7 @@ function checkPaths(args) {

type AllSupportedCliOptions = Options & MonitorOptions & TestOptions;

async function main() {
export async function main(): Promise<void> {
checkRuntime();

let res;
Expand Down Expand Up @@ -482,5 +477,3 @@ function validateOutputFile(
throw error;
}
}

callHandlingUnexpectedErrors(main, EXIT_CODES.ERROR);
2 changes: 1 addition & 1 deletion webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
entry: './src/cli/main.ts',
entry: './src/cli/index.ts',
target: 'node10',
output: {
clean: true,
Expand Down

0 comments on commit a8835ab

Please sign in to comment.