Skip to content

Commit 6b1e0dd

Browse files
committed
handle crwling errors
1 parent 56a5422 commit 6b1e0dd

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jscomplexity",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"preferGlobal" : "true",
55
"bin": {
66
"jscr": "generate-report.js"

sample.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var jscr = require('./index');
22

33
jscr('./utils', function(err, report){
44

5-
if (!err) console.log(report);
5+
console.log(report);
6+
console.log(err)
67

78
});

utils/crawl-complexity.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,49 @@
99

1010
var
1111
reportList = [],
12-
walker = walk.walk(path)
12+
walker = walk.walk(path),
13+
errStack = null
1314
;
1415

1516
walker
1617
.on("file", function (root, fileStats, next) {
1718

1819
var fileRef = require('path').normalize(root + "/" + fileStats.name);
19-
console.log(fileRef);
2020

2121
if( (/\.(js)$/i).test(fileRef) ) {
22-
2322

2423
fs.readFile(fileRef, 'utf8', function(err, data){
2524
if (err) throw new Error (err, fileRef);
26-
var report = cr.run(data);
27-
var toBeDisplayed = {
28-
path : fileRef,
29-
escapedPath : fileRef.replace(/\\/g, '\\'), // windows use
30-
complexity : report.aggregate.complexity.cyclomatic,
31-
lineNumber : report.aggregate.complexity.sloc.physical,
32-
maintainability : report.maintainability,
33-
halstead : {
34-
length : report.aggregate.complexity.halstead.length,
35-
vocabulary : Math.round(report.aggregate.complexity.halstead.vocabulary),
36-
difficulty : Math.round(report.aggregate.complexity.halstead.difficulty),
37-
bugs : Math.round(report.aggregate.complexity.halstead.bugs)
38-
}
39-
};
40-
reportList.push(toBeDisplayed);
41-
next();
25+
try {
26+
var report = cr.run(data);
27+
var toBeDisplayed = {
28+
path : fileRef,
29+
escapedPath : fileRef.replace(/\\/g, '\\'), // windows use
30+
complexity : report.aggregate.complexity.cyclomatic,
31+
lineNumber : report.aggregate.complexity.sloc.physical,
32+
maintainability : report.maintainability,
33+
halstead : {
34+
length : report.aggregate.complexity.halstead.length,
35+
vocabulary : Math.round(report.aggregate.complexity.halstead.vocabulary),
36+
difficulty : Math.round(report.aggregate.complexity.halstead.difficulty),
37+
bugs : Math.round(report.aggregate.complexity.halstead.bugs)
38+
}
39+
};
40+
reportList.push(toBeDisplayed);
41+
next();
42+
} catch(e){
43+
if (!errStack) errStack = [];
44+
errStack.push({ref : fileRef, error : e});
45+
next();
46+
}
47+
4248
});
4349

4450
} else { next(); }
4551

4652
})
47-
.on("error", function(){ cb(err); })
48-
.on("end", function(){ cb(null, reportList); })
53+
.on("error", function(){ cb(errStack, reportList); })
54+
.on("end", function(){ cb(errStack, reportList); })
4955
;
5056

5157
}

0 commit comments

Comments
 (0)