This repository has been archived by the owner on Aug 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from mc-zone/master
Prettify Error message and stack display, fix #32 .
- Loading branch information
Showing
8 changed files
with
122 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ language: node_js | |
node_js: | ||
- "4" | ||
- "6" | ||
- "7" | ||
- "8" | ||
script: npm run travis | ||
|
||
before_install: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
"use strict"; | ||
|
||
class MemoryFileSystemError extends Error { | ||
constructor(err, path, operation) { | ||
super(err, path); | ||
|
||
// Set `name` and `message` before call `Error.captureStackTrace` \ | ||
// so that we will obtain the correct 1st line of stack, like: | ||
// [Error]: [Message] | ||
this.name = this.constructor.name; | ||
var message = [`${err.code}:`, `${err.description},`]; | ||
// Add operation name and path into message, similar to node `fs` style. | ||
if(operation) { | ||
message.push(operation); | ||
} | ||
message.push(`\'${path}\'`); | ||
this.message = message.join(' '); | ||
|
||
this.code = err.code; | ||
this.errno = err.errno; | ||
this.path = path; | ||
this.operation = operation; | ||
|
||
if(Error.captureStackTrace) { | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} | ||
} | ||
|
||
module.exports = MemoryFileSystemError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
"use strict"; | ||
|
||
const normalize = require("./normalize"); | ||
|
||
const absoluteWinRegExp = /^[A-Z]:([\\\/]|$)/i; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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); | ||
firstLine.should.containEql(error.operation); | ||
done(); | ||
}); | ||
}); | ||
it("should work fine without operation name", function() { | ||
var errorData = { | ||
code:"ETEST", | ||
description:"testerror" | ||
}; | ||
var errorPath = "file"; | ||
var error = new MemoryFileSystemError(errorData, errorPath); | ||
error.message.should.startWith(error.code); | ||
error.stack.should.startWith(error.name); | ||
error.stack.should.containEql(error.message); | ||
error.stack.should.containEql(errorPath); | ||
}); | ||
}); |