├── .jshintignore ├── .travis.yml ├── index.js ├── .jshintrc ├── LICENSE ├── .gitignore ├── README.md ├── package.json └── test └── index.js /.jshintignore: -------------------------------------------------------------------------------- 1 | .gitignore -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.8" 4 | - "0.10" 5 | - "0.11" 6 | before_install: npm i npm@latest -g 7 | script: npm run travis 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var objectToString = Object.prototype.toString; 4 | var getPrototypeOf = Object.getPrototypeOf; 5 | var ERROR_TYPE = '[object Error]'; 6 | 7 | module.exports = function isError(err) { 8 | if (typeof err !== 'object') { 9 | return false; 10 | } 11 | if (err instanceof Error) { 12 | // Accept `AssertionError`s from the `assert` module that ships 13 | // with Node.js v6.1.0, compare issue #4. 14 | return true; 15 | } 16 | while (err) { 17 | if (objectToString.call(err) === ERROR_TYPE) { 18 | return true; 19 | } 20 | err = getPrototypeOf(err); 21 | } 22 | return false; 23 | }; 24 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "bitwise": false, 3 | "camelcase": true, 4 | "curly": false, 5 | "eqeqeq": true, 6 | "forin": true, 7 | "immed": true, 8 | "indent": 4, 9 | "latedef": "nofunc", 10 | "newcap": true, 11 | "noarg": true, 12 | "nonew": true, 13 | "plusplus": false, 14 | "quotmark": false, 15 | "regexp": false, 16 | "undef": true, 17 | "unused": true, 18 | "strict": false, 19 | "trailing": true, 20 | "node": true, 21 | "noempty": true, 22 | "maxdepth": 4, 23 | "maxparams": 4, 24 | "newcap": false, 25 | "globalstrict": true, 26 | "shadow": "outer", 27 | "globals": { 28 | "console": true, 29 | "Buffer": true, 30 | "setTimeout": true, 31 | "clearTimeout": true, 32 | "setInterval": true, 33 | "clearInterval": true 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 is-error. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | *.com 4 | *.class 5 | *.dll 6 | *.exe 7 | *.a 8 | *.o 9 | *.so 10 | *.node 11 | 12 | # Node Waf Byproducts # 13 | ####################### 14 | .lock-wscript 15 | build/ 16 | autom4te.cache/ 17 | 18 | # Node Modules # 19 | ################ 20 | # Better to let npm install these from the package.json defintion 21 | # rather than maintain this manually 22 | node_modules/ 23 | 24 | # Packages # 25 | ############ 26 | # it's better to unpack these files and commit the raw source 27 | # git has its own built in compression methods 28 | *.7z 29 | *.dmg 30 | *.gz 31 | *.iso 32 | *.jar 33 | *.rar 34 | *.tar 35 | *.zip 36 | 37 | # Logs and databases # 38 | ###################### 39 | *.log 40 | dump.rdb 41 | *.tap 42 | *.xml 43 | 44 | # OS generated files # 45 | ###################### 46 | .DS_Store? 47 | .DS_Store 48 | ehthumbs.db 49 | Icon? 50 | Thumbs.db 51 | coverage 52 | 53 | # Text Editor Byproducts # 54 | ########################## 55 | *.sw? 56 | .idea/ 57 | 58 | # Python object code 59 | ########################## 60 | *.py[oc] 61 | 62 | # All translation files # 63 | ######################### 64 | static/translations-s3/ 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # is-error 2 | 3 | 8 | 9 | 10 | 11 | Detect whether a value is an error 12 | 13 | ## Example 14 | 15 | ```js 16 | var isError = require("is-error"); 17 | 18 | console.log(isError(new Error('hi'))) // true 19 | console.log(isError({ message: 'hi' })) // false 20 | ``` 21 | 22 | ## Docs 23 | 24 | ### `var bool = isError(maybeErr)` 25 | 26 | ```hs 27 | is-error := (maybeErr: Any) => Boolean 28 | ``` 29 | 30 | `isError` returns a boolean. it will detect whether the argument 31 | is an error or not. 32 | 33 | ## Installation 34 | 35 | `npm install is-error` 36 | 37 | ## Tests 38 | 39 | `npm test` 40 | 41 | ## Contributors 42 | 43 | - Raynos 44 | 45 | ## MIT Licensed 46 | 47 | [build-png]: https://secure.travis-ci.org/Raynos/is-error.png 48 | [build]: https://travis-ci.org/Raynos/is-error 49 | [cover-png]: https://coveralls.io/repos/Raynos/is-error/badge.png 50 | [cover]: https://coveralls.io/r/Raynos/is-error 51 | [dep-png]: https://david-dm.org/Raynos/is-error.png 52 | [dep]: https://david-dm.org/Raynos/is-error 53 | [npm-png]: https://nodei.co/npm/is-error.png?stars&downloads 54 | [npm]: https://nodei.co/npm/is-error 55 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { "name": "is-error", 2 | "version": "2.2.2", 3 | "description": "Detect whether a value is an error", 4 | "keywords": [], 5 | "author": "Raynos ", 6 | "repository": "git://github.com/mk-pmb/is-error-js.git", 7 | "main": "index", 8 | "homepage": "https://github.com/mk-pmb/is-error-js", 9 | "bugs": { 10 | "url": "https://github.com/mk-pmb/is-error-js/issues" 11 | }, 12 | "contributors": [ 13 | "Raynos", 14 | "M.K. (https://github.com/mk-pmb)" 15 | ], 16 | "dependencies": {}, 17 | "devDependencies": { 18 | "coveralls": "^2.10.0", 19 | "istanbul": "^0.3.5", 20 | "lint-trap": "^1.0.0", 21 | "opn": "^1.0.1", 22 | "pre-commit": "0.0.11", 23 | "tap-spec": "^2.1.1", 24 | "tape": "^3.4.0" 25 | }, 26 | "license": "MIT", 27 | "scripts": { 28 | "test": "npm run jshint -s && npm run cover -s", 29 | "unit-test": "node test/index.js | tap-spec", 30 | "jshint": "lint-trap .", 31 | "cover": "istanbul cover --report html --print detail -- test/index.js && npm run check-cover -s", 32 | "check-cover": "istanbul check-coverage --branches=100 --lines=100 --functions=100", 33 | "view-cover": "opn ./coverage/index.html", 34 | "travis": "npm run cover -s && istanbul report lcov && ((cat coverage/lcov.info | coveralls) || true)" 35 | }, 36 | "engine": { 37 | "node": ">= 0.8.x" 38 | }, 39 | "pre-commit": [ 40 | "test" 41 | ], 42 | "pre-commit.silent": true, 43 | "ngen-version": "5.1.0" 44 | } 45 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var test = require('tape'); 4 | var vm = require('vm'); 5 | 6 | var isError = require('../index.js'); 7 | 8 | test('isError is a function', function t(assert) { 9 | assert.equal(typeof isError, 'function'); 10 | assert.end(); 11 | }); 12 | 13 | test('returns true for error', function t(assert) { 14 | assert.equal(isError(new Error('foo')), true); 15 | assert.equal(isError(Error('foo')), true); 16 | assert.end(); 17 | }); 18 | 19 | test('returns false for non-error', function t(assert) { 20 | assert.equal(isError(null), false); 21 | assert.equal(isError(undefined), false); 22 | assert.equal(isError({message: 'hi'}), false); 23 | assert.equal(isError(true), false); 24 | assert.equal(isError(false), false); 25 | assert.equal(isError(1), false); 26 | assert.equal(isError('string'), false); 27 | assert.end(); 28 | }); 29 | 30 | test('errors that inherit from Error', function t(assert) { 31 | var error = Object.create(new Error()); 32 | assert.equal(isError(error), true); 33 | assert.end(); 34 | }); 35 | 36 | test('errors from other contexts', function t(assert) { 37 | var error = vm.runInNewContext('new Error()'); 38 | assert.equal(isError(error), true); 39 | assert.end(); 40 | }); 41 | 42 | test('errors that inherit from Error in another context', function t(assert) { 43 | var error = vm.runInNewContext('Object.create(new Error())'); 44 | assert.equal(isError(error), true); 45 | assert.end(); 46 | }); 47 | --------------------------------------------------------------------------------