Skip to content

Commit

Permalink
test: initial test for onlyInstancesOf
Browse files Browse the repository at this point in the history
  • Loading branch information
saintsebastian committed Aug 7, 2017
1 parent 63916af commit 9b9e16c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 13 additions & 0 deletions __tests__/errors.test.js
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);
}));
});
});
12 changes: 7 additions & 5 deletions errors.js
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);
}
}
};

0 comments on commit 9b9e16c

Please sign in to comment.