-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: initial test for onlyInstancesOf
- Loading branch information
1 parent
63916af
commit 9b9e16c
Showing
2 changed files
with
20 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use strict"; | ||
|
||
const UsageError = require("../errors").UsageError; | ||
const onlyInstancesOf = require("../errors").onlyInstancesOf; | ||
|
||
describe("onlyInstancesOf", () => { | ||
test("catches specified error", () => { | ||
return Promise.reject(new UsageError("fake usage error")) | ||
.catch(onlyInstancesOf(UsageError, (error) => { | ||
expect(error).toBeInstanceOf(UsageError); | ||
})); | ||
}); | ||
}); |
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,18 +1,20 @@ | ||
const ES6Error = require("es6-error"); | ||
|
||
exports.onlyInstancesOf = function(errorType, handler) { | ||
return(error) => { | ||
exports.onlyInstancesOf = function (errorType, handler) { | ||
return (error) => { | ||
if (error instanceof errorType) { | ||
return handler(error); | ||
// eslint-disable-next-line no-else-return | ||
} else { | ||
console.log(error.stack); | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
exports.UsageError = class UsageError extends ES6Error { | ||
// eslint-disable-next-line no-useless-constructor | ||
constructor(message) { | ||
super(message); | ||
} | ||
} | ||
}; |