Skip to content

Commit

Permalink
Add a DepGraphCycleError with cyclePath property
Browse files Browse the repository at this point in the history
  • Loading branch information
jhugman committed Nov 13, 2018
1 parent bcaed99 commit 7c37288
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
25 changes: 24 additions & 1 deletion lib/dep_graph.js
Expand Up @@ -25,7 +25,7 @@ function createDFS(edges, leavesOnly, result, circular) {
} else if (currentPath.indexOf(node) >= 0) {
currentPath.push(node);
if (!circular) {
throw new Error('Dependency Cycle Found: ' + currentPath.join(' -> '));
throw new DepGraphCycleError(currentPath);
}
}
});
Expand Down Expand Up @@ -242,3 +242,26 @@ DepGraph.prototype = {
}
}
};

/**
* Cycle error, including the path of the cycle.
*/
var DepGraphCycleError = exports.DepGraphCycleError = function (cyclePath) {
var message = 'Dependency Cycle Found: ' + cyclePath.join(' -> ')
var instance = new Error(message);
instance.cyclePath = cyclePath;
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
if (Error.captureStackTrace) {
Error.captureStackTrace(instance, DepGraphCycleError);
}
return instance;
}
DepGraphCycleError.prototype = Object.create(Error.prototype, {
constructor: {
value: Error,
enumerable: false,
writable: true,
configurable: true
}
});
Object.setPrototypeOf(DepGraphCycleError, Error);
4 changes: 4 additions & 0 deletions lib/index.d.ts
Expand Up @@ -86,4 +86,8 @@ declare module 'dependency-graph' {
*/
overallOrder(leavesOnly?: boolean): string[];
}

export class DepGraphCycleError extends Error {
cyclePath: string[];
}
}
32 changes: 27 additions & 5 deletions specs/dep_graph_spec.js
@@ -1,4 +1,5 @@
var DepGraph = require('../lib/dep_graph').DepGraph;
var dep_graph = require('../lib/dep_graph');
var DepGraph = dep_graph.DepGraph;

describe('DepGraph', function () {

Expand Down Expand Up @@ -161,7 +162,7 @@ describe('DepGraph', function () {

expect(function () {
graph.dependenciesOf('b');
}).toThrow(new Error('Dependency Cycle Found: b -> c -> a -> b'));
}).toThrow(new dep_graph.DepGraphCycleError(['b', 'c', 'a', 'b']));
});

it('should allow cycles when configured', function () {
Expand Down Expand Up @@ -196,7 +197,7 @@ describe('DepGraph', function () {

expect(function () {
graph.overallOrder();
}).toThrow(new Error('Dependency Cycle Found: a -> b -> c -> a'));
}).toThrow(new dep_graph.DepGraphCycleError(['a', 'b', 'c', 'a']));
});

it('should detect cycles in overall order when all nodes have dependants (incoming edges)', function () {
Expand All @@ -212,7 +213,7 @@ describe('DepGraph', function () {

expect(function () {
graph.overallOrder();
}).toThrow(new Error('Dependency Cycle Found: a -> b -> c -> a'));
}).toThrow(new dep_graph.DepGraphCycleError(['a', 'b', 'c', 'a']));
});

it('should detect cycles in overall order when there are several ' +
Expand All @@ -232,7 +233,7 @@ describe('DepGraph', function () {

expect(function () {
graph.overallOrder();
}).toThrow(new Error('Dependency Cycle Found: b_1 -> b_2 -> b_3 -> b_1'));
}).toThrow(new dep_graph.DepGraphCycleError(['b_1', 'b_2', 'b_3', 'b_1']));
});

it('should retrieve dependencies and dependants in the correct order', function () {
Expand Down Expand Up @@ -387,3 +388,24 @@ describe('DepGraph', function () {
expect(graph.getNodeData('a') === cloned.getNodeData('a')).toBe(false);
});
});

describe('DepGraphCycleError', function() {
var DepGraphCycleError = dep_graph.DepGraphCycleError;

it('should have a message', function() {
var err = new DepGraphCycleError(['a', 'b', 'c', 'a']);
expect(err.message).toEqual('Dependency Cycle Found: a -> b -> c -> a');
});

it('should be an instanceof DepGraphCycleError', function() {
var err = new DepGraphCycleError(['a', 'b', 'c', 'a']);
expect(err instanceof DepGraphCycleError).toBe(true);
expect(err instanceof Error).toBe(true);
});

it('should have a cyclePath', function() {
var cyclePath = ['a', 'b', 'c', 'a'];
var err = new DepGraphCycleError(cyclePath);
expect(err.cyclePath).toEqual(cyclePath);
});
});

0 comments on commit 7c37288

Please sign in to comment.