Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
Fix stack message missing caused by visit and invoke stack getter bef…
Browse files Browse the repository at this point in the history
…ore call `captureStackTrace` (found in node V6.x).

Use strict mode.

Prettify tests.
  • Loading branch information
mc-zone committed May 31, 2017
1 parent 833cf1d commit 59849c5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 37 deletions.
16 changes: 12 additions & 4 deletions lib/MemoryFileSystemError.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const operationMatchReg = new RegExp("at\\sMemoryFileSystem\\.([^\\s_]+)\\s");

class MemoryFileSystemError extends Error {
Expand Down Expand Up @@ -29,10 +31,16 @@ class MemoryFileSystemError extends Error {
}
}
findOperation() {
if(!this.stack) return null;
var stacks = this.stack.split(/[\r\n|\n][\s]*/);
var operation;
// use a new obj to capture and inspect stack, because the message \
// isn't ready yet, shouldn't invoke stack getter at this time.
var captureObj = {};
if(Error.captureStackTrace) {
Error.captureStackTrace(captureObj);
}
if(!captureObj.stack) return null;
var stacks = captureObj.stack.split(/[\r\n|\n][\s]*/);
var findDepth = 1;
var operation;
while(!operation && findDepth < stacks.length) {
var stack = stacks[findDepth];
var operationMatch = stack.match(operationMatchReg);
Expand All @@ -42,7 +50,7 @@ class MemoryFileSystemError extends Error {
findDepth += 1;
}

return operation || null;
return operation;
}
}

Expand Down
33 changes: 0 additions & 33 deletions test/MemoryFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,6 @@ describe("files", function() {
});
});
describe("errors", function() {
function catchError(fn) {
try {
fn();
} catch(e) {
return e;
}
return null;
}

it("should fail on invalid paths", function() {
var fs = new MemoryFileSystem();
fs.mkdirpSync("/test/a/b/c");
Expand Down Expand Up @@ -152,30 +143,6 @@ describe("errors", function() {
err.should.be.instanceof(Error);
});
});
it("should include the path in Error message", function(done) {
var fs = new MemoryFileSystem();
var invalidPath = "/nonexist/file";
var error = catchError(function() {
fs.statSync(invalidPath);
});
error.message.indexOf(invalidPath).should.be.above(-1);

fs.readFile(invalidPath, function(err) {
err.message.indexOf(invalidPath).should.be.above(-1);
done();
});
});
it("should use correct error name and message in the first line of Error stack", function(done) {
var fs = new MemoryFileSystem();
fs.unlink("/test/abcd", function(error) {
error.should.be.instanceof(Error);
var firstLine = error.stack.split(/\r\n|\n/)[0];
firstLine.should.startWith(error.name);
firstLine.indexOf(error.code).should.be.above(-1);
firstLine.indexOf(error.message).should.be.above(-1);
done();
});
});
it("should fail incorrect arguments", function() {
var fs = new MemoryFileSystem();
(function() {
Expand Down
51 changes: 51 additions & 0 deletions test/MemoryFileSystemError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var bl = require("bl");
var should = require("should");
var MemoryFileSystem = require("../lib/MemoryFileSystem");
var MemoryFileSystemError = require("../lib/MemoryFileSystemError");

describe("error", function() {
function catchError(fn) {
try {
fn();
} catch(e) {
return e;
}
return null;
}

it("should include the path in Error message", function(done) {
var fs = new MemoryFileSystem();
var invalidPath = "/nonexist/file";
var error = catchError(function() {
fs.statSync(invalidPath);
});
error.message.should.containEql(invalidPath);

fs.readFile(invalidPath, function(err) {
err.message.should.containEql(invalidPath);
done();
});
});
it("should use correct error message in the first line of Error stack", function(done) {
var fs = new MemoryFileSystem();
fs.unlink("/test/abcd", function(error) {
error.should.be.instanceof(Error);
error.stack.should.startWith(error.name);

var firstLine = error.stack.split(/\r\n|\n/)[0];
firstLine.should.containEql(error.code);
firstLine.should.containEql(error.message);
done();
});
});
it("should work fine without path and operation", function() {
var errorData = {
code:"ETEST",
description:"testerror",
};
var error = new MemoryFileSystemError(errorData);
error.message.should.startWith(error.code);
error.stack.should.startWith(error.name);
error.stack.should.containEql(error.message);
});
});

0 comments on commit 59849c5

Please sign in to comment.