Skip to content

Commit 2010cb6

Browse files
authored
Merge pull request #6 from snyk/feat/custom-validation-errors
feat: custom ValidationError
2 parents 3302c6d + dd1e97a commit 2010cb6

File tree

7 files changed

+46
-2
lines changed

7 files changed

+46
-2
lines changed

src/core/create-from-json.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as semver from 'semver';
33
import * as graphlib from 'graphlib';
44

55
import { DepGraph, DepGraphData, GraphNode } from './types';
6+
import { ValidationError } from './errors';
67
import { validateGraph } from './validate-graph';
78
import { DepGraphImpl } from './dep-graph';
89

@@ -52,7 +53,7 @@ export function createFromJSON(depGraphData: DepGraphData): DepGraph {
5253

5354
function assert(condition: boolean, msg: string) {
5455
if (!condition) {
55-
throw new Error(msg);
56+
throw new ValidationError(msg);
5657
}
5758
}
5859

src/core/errors/custom-error.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class CustomError extends Error {
2+
constructor(message) {
3+
super(message);
4+
Object.setPrototypeOf(this, CustomError.prototype);
5+
Error.captureStackTrace(this, this.constructor);
6+
this.name = this.constructor.name;
7+
}
8+
}

src/core/errors/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { ValidationError } from './validation-error';
2+
3+
export {
4+
ValidationError,
5+
};

src/core/errors/validation-error.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { CustomError } from './custom-error';
2+
3+
export class ValidationError extends CustomError {
4+
constructor(message) {
5+
super(message);
6+
Object.setPrototypeOf(this, ValidationError.prototype);
7+
}
8+
}

src/core/validate-graph.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as _ from 'lodash';
22
import * as graphlib from 'graphlib';
3+
import { ValidationError } from './errors';
34

45
function assert(condition: boolean, msg: string) {
56
if (!condition) {
6-
throw new Error(msg);
7+
throw new ValidationError(msg);
78
}
89
}
910

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ import 'source-map-support/register';
33
export { DepGraphData, DepGraph, PkgManager } from './core/types';
44
export { createFromJSON } from './core/create-from-json';
55

6+
import * as Errors from './core/errors';
7+
export { Errors };
8+
69
import * as legacy from './legacy';
710
export { legacy };

test/core/create-from-json.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ test('fromJSON too old schemaVersion', async () => {
181181

182182
const go = () => depGraphLib.createFromJSON(graphJson);
183183
expect(go).toThrow(/schemaVersion/);
184+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
184185
});
185186

186187
test('fromJSON too new schemaVersion', async () => {
@@ -190,6 +191,7 @@ test('fromJSON too new schemaVersion', async () => {
190191

191192
const go = () => depGraphLib.createFromJSON(graphJson);
192193
expect(go).toThrow(/schemaVersion/);
194+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
193195
});
194196

195197
test('fromJSON missing root', async () => {
@@ -204,6 +206,7 @@ test('fromJSON missing root', async () => {
204206

205207
const go = () => depGraphLib.createFromJSON(graphJson);
206208
expect(go).toThrow(/root/);
209+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
207210
});
208211

209212
test('fromJSON missing pkgManager.name', async () => {
@@ -213,6 +216,7 @@ test('fromJSON missing pkgManager.name', async () => {
213216

214217
const go = () => depGraphLib.createFromJSON(graphJson);
215218
expect(go).toThrow(/pkgManager\.name/);
219+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
216220
});
217221

218222
test('fromJSON missing pkgManager', async () => {
@@ -222,6 +226,7 @@ test('fromJSON missing pkgManager', async () => {
222226

223227
const go = () => depGraphLib.createFromJSON(graphJson);
224228
expect(go).toThrow(/pkgManager/);
229+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
225230
});
226231

227232
test('fromJSON root pkg id doesnt match name@version', async () => {
@@ -255,6 +260,7 @@ test('fromJSON root pkg id doesnt match name@version', async () => {
255260

256261
const go = () => depGraphLib.createFromJSON(graphJson);
257262
expect(go).toThrow(/name@version/);
263+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
258264
});
259265

260266
test('fromJSON with a cycle', async () => {
@@ -359,6 +365,7 @@ test('fromJSON root is not really root', async () => {
359365

360366
const go = () => depGraphLib.createFromJSON(graphJson);
361367
expect(go).toThrow(/root/);
368+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
362369
});
363370

364371
test('fromJSON a pkg is not reachable from root', async () => {
@@ -398,6 +405,7 @@ test('fromJSON a pkg is not reachable from root', async () => {
398405

399406
const go = () => depGraphLib.createFromJSON(graphJson);
400407
expect(go).toThrow(/reach/);
408+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
401409
});
402410

403411
test('fromJSON root is not really root', async () => {
@@ -439,6 +447,7 @@ test('fromJSON root is not really root', async () => {
439447

440448
const go = () => depGraphLib.createFromJSON(graphJson);
441449
expect(go).toThrow(/root/);
450+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
442451
});
443452

444453
test('fromJSON a pkg without an instance', async () => {
@@ -473,6 +482,7 @@ test('fromJSON a pkg without an instance', async () => {
473482

474483
const go = () => depGraphLib.createFromJSON(graphJson);
475484
expect(go).toThrow(/instance/);
485+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
476486
});
477487

478488
test('fromJSON an instance without a pkg', async () => {
@@ -512,6 +522,7 @@ test('fromJSON an instance without a pkg', async () => {
512522

513523
const go = () => depGraphLib.createFromJSON((graphJson as any) as depGraphLib.DepGraphData);
514524
expect(go).toThrow(/instance/);
525+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
515526
});
516527

517528
test('fromJSON an instance points to non-existing pkgId', async () => {
@@ -552,6 +563,7 @@ test('fromJSON an instance points to non-existing pkgId', async () => {
552563

553564
const go = () => depGraphLib.createFromJSON(graphJson);
554565
expect(go).toThrow(/exist/);
566+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
555567
});
556568

557569
test('fromJSON root has several instances', async () => {
@@ -592,6 +604,7 @@ test('fromJSON root has several instances', async () => {
592604

593605
const go = () => depGraphLib.createFromJSON(graphJson);
594606
expect(go).toThrow(/root/);
607+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
595608
});
596609

597610
test('fromJSON a pkg missing info field', async () => {
@@ -626,6 +639,7 @@ test('fromJSON a pkg missing info field', async () => {
626639
const go = () => depGraphLib.createFromJSON((graphJson as any) as depGraphLib.DepGraphData);
627640
expect(go).toThrow(/\.info/);
628641
expect(go).toThrow(/^((?!(of undefined)).)*$/);
642+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
629643
});
630644

631645
test('fromJSON a pkg missing name field', async () => {
@@ -659,6 +673,7 @@ test('fromJSON a pkg missing name field', async () => {
659673

660674
const go = () => depGraphLib.createFromJSON((graphJson as any) as depGraphLib.DepGraphData);
661675
expect(go).toThrow(/name/);
676+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
662677
});
663678

664679
test('fromJSON a pkg missing version field', async () => {
@@ -728,6 +743,7 @@ test('fromJSON pkg-id is not name@version', async () => {
728743

729744
const go = () => depGraphLib.createFromJSON(graphJson);
730745
expect(go).toThrow(/name/);
746+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
731747
});
732748

733749
test('fromJSON duplicate node-id', async () => {
@@ -766,6 +782,7 @@ test('fromJSON duplicate node-id', async () => {
766782

767783
const go = () => depGraphLib.createFromJSON(graphJson);
768784
expect(go).toThrow(/node.*same id/);
785+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
769786
});
770787

771788
test('fromJSON duplicate pkg-id', async () => {
@@ -800,4 +817,5 @@ test('fromJSON duplicate pkg-id', async () => {
800817

801818
const go = () => depGraphLib.createFromJSON(graphJson);
802819
expect(go).toThrow(/pkg.*same id/);
820+
expect(go).toThrow(depGraphLib.Errors.ValidationError);
803821
});

0 commit comments

Comments
 (0)