Skip to content

Commit

Permalink
feat: error/is-error
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Apr 3, 2019
1 parent f89f14a commit 4d6b899
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ ensureRegExp(/foo/); // /foo/
ensureRegExp("foo"); // Thrown TypeError: null is not a regular expression object
```

#### Error

The JavaScript _Error_ instance

##### `error/is`

```javascript
const isError = require("type/error/is");

isError(new Error()); // true
isError({ mesage: "Fake error" }); // false
```

### Tests

$ npm test
Expand Down
19 changes: 19 additions & 0 deletions error/is.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

var isPrototype = require("../prototype/is");

var objectToString = Object.prototype.toString
, objectTaggedString = objectToString.call(new Error());

module.exports = function (value) {
if (!value) return false;

// Sanity check (reject objects which do not expose common Error interface)
if (typeof value.message !== "string") return false;

// Ensure its native Error object (has [[ErrorData]] slot)
// Note: it's not 100% precise as string tag may be overriden
if (objectToString.call(value) !== objectTaggedString) return false;

return !isPrototype(value);
};
42 changes: 42 additions & 0 deletions test/error/is.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict";

var assert = require("chai").assert
, isError = require("../../error/is");

describe("error/is", function () {
it("Should return true on error", function () { assert.equal(isError(new Error()), true); });

it("Should return false on native error with no common API exposed", function () {
var value = new Error();
value.message = null;
assert.equal(isError(value), false);
});
it("Should return false on Error.prototype", function () {
assert.equal(isError(Error.prototype), false);
});

it("Should return false on plain object", function () { assert.equal(isError({}), false); });
it("Should return false on function", function () {
assert.equal(isError(function () { return true; }), false);
});

it("Should return false on array", function () { assert.equal(isError([]), false); });
if (typeof Object.create === "function") {
it("Should return false on object with no prototype", function () {
assert.equal(isError(Object.create(null)), false);
});
}
it("Should return false on string", function () { assert.equal(isError("foo"), false); });
it("Should return false on empty string", function () { assert.equal(isError(""), false); });
it("Should return false on number", function () { assert.equal(isError(123), false); });
it("Should return false on NaN", function () { assert.equal(isError(NaN), false); });
it("Should return false on boolean", function () { assert.equal(isError(true), false); });
if (typeof Symbol === "function") {
it("Should return false on symbol", function () {
assert.equal(isError(Symbol("foo")), false);
});
}

it("Should return false on null", function () { assert.equal(isError(null), false); });
it("Should return false on undefined", function () { assert.equal(isError(void 0), false); });
});

0 comments on commit 4d6b899

Please sign in to comment.