├── .gitignore ├── .npmrc ├── index.js ├── .travis.yml ├── src └── always-error-spec.js ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | registry=http://registry.npmjs.org/ 2 | save-exact=true 3 | progress=false 4 | 5 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | function alwaysError (x) { 2 | if (x instanceof Error) { 3 | return x 4 | } 5 | 6 | if (typeof x === 'string') { 7 | return new Error(x) 8 | } 9 | 10 | throw new Error('Cannot convert ' + x + ' to Error instance!') 11 | } 12 | 13 | module.exports = alwaysError 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | cache: 4 | directories: 5 | - node_modules 6 | notifications: 7 | email: false 8 | node_js: 9 | - '4' 10 | before_install: 11 | - npm i -g npm@^2.0.0 12 | before_script: 13 | - npm prune 14 | after_success: 15 | - npm run semantic-release 16 | branches: 17 | except: 18 | - "/^v\\d+\\.\\d+\\.\\d+$/" 19 | -------------------------------------------------------------------------------- /src/always-error-spec.js: -------------------------------------------------------------------------------- 1 | const la = require('lazy-ass') 2 | const is = require('check-more-types') 3 | 4 | /* global describe, it */ 5 | describe('always error', () => { 6 | const always = require('..') 7 | 8 | it('is a function', () => { 9 | la(is.fn(always)) 10 | }) 11 | 12 | it('leaves Error unchanged', () => { 13 | const e = new Error('foo') 14 | const f = always(e) 15 | la(e === f) 16 | }) 17 | 18 | it('creates Error from string', () => { 19 | const e = always('foo') 20 | la(is.error(e), e) 21 | la(e.message === 'foo', e) 22 | }) 23 | 24 | it('raises error for non strings', () => { 25 | la(is.raises(() => { 26 | always(42) 27 | })) 28 | }) 29 | }) 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "always-error", 3 | "version": "0.0.0-semantic-release", 4 | "description": "Ensure the argument is an instance of Error or convert it", 5 | "author": "Gleb Bahmutov ", 6 | "bugs": "https://github.com/bahmutov/always-error/issues", 7 | "config": { 8 | "pre-git": { 9 | "commit-msg": "simple", 10 | "pre-commit": [ 11 | "npm test", 12 | "npm run ban" 13 | ], 14 | "pre-push": [ 15 | "npm run secure", 16 | "npm run license", 17 | "npm run ban -- --all", 18 | "npm run size" 19 | ], 20 | "post-commit": [], 21 | "post-merge": [] 22 | } 23 | }, 24 | "files": [ 25 | "index.js", 26 | "src/*.js", 27 | "!src/*-spec.js" 28 | ], 29 | "homepage": "https://github.com/bahmutov/always-error#readme", 30 | "keywords": [ 31 | "convert", 32 | "crash", 33 | "error", 34 | "report", 35 | "utility" 36 | ], 37 | "license": "MIT", 38 | "main": "index.js", 39 | "repository": { 40 | "type": "git", 41 | "url": "https://github.com/bahmutov/always-error.git" 42 | }, 43 | "scripts": { 44 | "ban": "ban", 45 | "format": "standard-format -w index.js src/*.js", 46 | "issues": "git-issues", 47 | "license": "license-checker --production --onlyunknown --csv", 48 | "lint": "standard --verbose index.js src/*.js", 49 | "pretest": "npm run format && npm run lint", 50 | "secure": "nsp check", 51 | "size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";", 52 | "test": "npm run unit", 53 | "unit": "mocha src/*-spec.js", 54 | "commit": "commit-wizard", 55 | "semantic-release": "semantic-release pre && npm publish && semantic-release post" 56 | }, 57 | "devDependencies": { 58 | "ban-sensitive-files": "1.8.2", 59 | "check-more-types": "2.13.0", 60 | "git-issues": "1.2.0", 61 | "lazy-ass": "1.4.0", 62 | "license-checker": "5.1.0", 63 | "mocha": "2.4.5", 64 | "nsp": "2.2.2", 65 | "pre-git": "3.7.1", 66 | "standard": "6.0.8", 67 | "standard-format": "2.1.1", 68 | "semantic-release": "^4.3.5" 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # always-error 2 | 3 | > Ensure the argument is an instance of Error or convert it 4 | 5 | [![NPM][npm-icon] ][npm-url] 6 | 7 | [![Build status][ci-image] ][ci-url] 8 | [![semantic-release][semantic-image] ][semantic-url] 9 | [![js-standard-style][standard-image]][standard-url] 10 | ![no sudden unpublish](https://img.shields.io/badge/no%20sudden-unpublish%20%E2%9A%93-ff69b4.svg) 11 | 12 | ## Install and use 13 | 14 | ```sh 15 | npm install --save always-error 16 | ``` 17 | 18 | ```js 19 | const alwaysError = require('always-error') 20 | function reportError(x) { 21 | // hmm, is x an instance of Error or not? 22 | report(alwaysError(x)) 23 | } 24 | ``` 25 | 26 | If `x` is already an instance of Error, then it is passed unchanged, otherwise converted into one. 27 | 28 | ### Small print 29 | 30 | Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2016 31 | 32 | 33 | * [@bahmutov](https://twitter.com/bahmutov) 34 | * [glebbahmutov.com](http://glebbahmutov.com) 35 | * [blog](http://glebbahmutov.com/blog) 36 | 37 | 38 | License: MIT - do anything with the code, but don't blame me if it does not work. 39 | 40 | Support: if you find any problems with this module, email / tweet / 41 | [open issue](https://github.com/bahmutov/always-error/issues) on Github 42 | 43 | ## MIT License 44 | 45 | Copyright (c) 2016 Gleb Bahmutov <gleb.bahmutov@gmail.com> 46 | 47 | Permission is hereby granted, free of charge, to any person 48 | obtaining a copy of this software and associated documentation 49 | files (the "Software"), to deal in the Software without 50 | restriction, including without limitation the rights to use, 51 | copy, modify, merge, publish, distribute, sublicense, and/or sell 52 | copies of the Software, and to permit persons to whom the 53 | Software is furnished to do so, subject to the following 54 | conditions: 55 | 56 | The above copyright notice and this permission notice shall be 57 | included in all copies or substantial portions of the Software. 58 | 59 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 60 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 61 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 62 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 63 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 64 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 65 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 66 | OTHER DEALINGS IN THE SOFTWARE. 67 | 68 | [npm-icon]: https://nodei.co/npm/always-error.png?downloads=true 69 | [npm-url]: https://npmjs.org/package/always-error 70 | [ci-image]: https://travis-ci.org/bahmutov/always-error.png?branch=master 71 | [ci-url]: https://travis-ci.org/bahmutov/always-error 72 | [semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg 73 | [semantic-url]: https://github.com/semantic-release/semantic-release 74 | [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg 75 | [standard-url]: http://standardjs.com/ 76 | --------------------------------------------------------------------------------