Skip to content

Commit

Permalink
feat: add monitor errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Jun 25, 2019
1 parent 7b1fe64 commit d3a3644
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/cli/commands/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import * as spinner from '../../lib/spinner';
import * as detect from '../../lib/detect';
import * as plugins from '../../lib/plugins';
import {ModuleInfo} from '../../lib/module-info'; // TODO(kyegupov): fix import
import { SingleDepRootResult, MultiDepRootsResult, isMultiResult, MonitorError, MonitorOptions } from '../../lib/types';
import { SingleDepRootResult, MultiDepRootsResult, isMultiResult, MonitorOptions } from '../../lib/types';
import { MethodArgs, ArgsOptions } from '../args';
import { maybePrintDeps } from '../../lib/print-deps';
import * as analytics from '../../lib/analytics';
import {MonitorError} from '../../lib/errors';

const SEPARATOR = '\n-------------------------------------------------------\n';

Expand Down
12 changes: 12 additions & 0 deletions src/lib/errors/connection-timeout-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {CustomError} from './custom-error';

export class ConnectionTimeoutError extends CustomError {
private static ERROR_MESSAGE: string =
'Connection timeout.';

constructor() {
super(ConnectionTimeoutError.ERROR_MESSAGE);
this.code = 504;
this.userMessage = ConnectionTimeoutError.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 @@ -3,3 +3,5 @@ export {FileFlagBadInputError} from './file-flag-bad-input';
export {MissingTargetFileError} from './missing-targetfile-error';
export {NoSupportedManifestsFoundError} from './no-supported-manifests-found';
export {CustomError} from './custom-error';
export {MonitorError} from './monitor-error';
export {ConnectionTimeoutError} from './connection-timeout-error';
13 changes: 13 additions & 0 deletions src/lib/errors/monitor-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {CustomError} from './custom-error';

export class MonitorError extends CustomError {
private static ERROR_MESSAGE: string =
'Server returned unexpected error for the monitor request. ';

constructor(errorCode, message) {
super(MonitorError.ERROR_MESSAGE +
`Status code: ${errorCode}, response: ${message}`);
this.code = errorCode;
this.userMessage = message;
}
}
17 changes: 9 additions & 8 deletions src/lib/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import * as os from 'os';
import * as _ from 'lodash';
import {isCI} from './is-ci';
import * as analytics from './analytics';
import { SingleDepRootResult, MonitorError, DepTree } from './types';
import { SingleDepRootResult, DepTree } from './types';
import * as projectMetadata from './project-metadata';
import * as path from 'path';
import {MonitorError, ConnectionTimeoutError} from './errors';

// TODO(kyegupov): clean up the type, move to snyk-cli-interface repository

Expand Down Expand Up @@ -125,14 +126,14 @@ export async function monitor(root, meta, info: SingleDepRootResult, targetFile)
if (res.statusCode === 200 || res.statusCode === 201) {
resolve(body);
} else {
const e = new MonitorError('Server returned unexpected error for the monitor request. ' +
`Status code: ${res.statusCode}, response: ${res.body.userMessage || res.body.message}`);
e.code = res.statusCode;
e.userMessage = body && body.userMessage;
if (!e.userMessage && res.statusCode === 504) {
e.userMessage = 'Connection Timeout';
let err;
const userMessage = body && body.userMessage;
if (!userMessage && res.statusCode === 504) {
err = new ConnectionTimeoutError();
} else {
err = new MonitorError(res.statusCode, userMessage);
}
reject(e);
reject(err);
}
});
});
Expand Down
5 changes: 0 additions & 5 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ export function isMultiResult(pet: SingleDepRootResult | MultiDepRootsResult): p
return !!(pet as MultiDepRootsResult).depRoots;
}

export class MonitorError extends Error {
public code?: number;
public userMessage?: string;
}

export interface TestOptions {
traverseNodeModules: boolean;
interactive: boolean;
Expand Down

0 comments on commit d3a3644

Please sign in to comment.