├── .editorconfig ├── .gitattributes ├── .gitignore ├── .jshintrc ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "curly": true, 6 | "immed": true, 7 | "newcap": true, 8 | "noarg": true, 9 | "undef": true, 10 | "unused": "vars", 11 | "strict": true 12 | } 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 'iojs' 4 | - '0.12' 5 | - '0.10' 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var captureStackTrace = require('capture-stack-trace'); 3 | 4 | function inherits(ctor, superCtor) { 5 | ctor.super_ = superCtor; 6 | ctor.prototype = Object.create(superCtor.prototype, { 7 | constructor: { 8 | value: ctor, 9 | enumerable: false, 10 | writable: true, 11 | configurable: true 12 | } 13 | }); 14 | } 15 | 16 | module.exports = function createErrorClass(className, setup) { 17 | if (typeof className !== 'string') { 18 | throw new TypeError('Expected className to be a string'); 19 | } 20 | 21 | if (/[^0-9a-zA-Z_$]/.test(className)) { 22 | throw new Error('className contains invalid characters'); 23 | } 24 | 25 | setup = setup || function (message) { 26 | this.message = message; 27 | }; 28 | 29 | var ErrorClass = function () { 30 | Object.defineProperty(this, 'name', { 31 | configurable: true, 32 | value: className, 33 | writable: true 34 | }); 35 | 36 | captureStackTrace(this, this.constructor); 37 | 38 | setup.apply(this, arguments); 39 | }; 40 | 41 | inherits(ErrorClass, Error); 42 | 43 | return ErrorClass; 44 | }; 45 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Vsevolod Strukchinsky (github.com/floatdrop) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "create-error-class", 3 | "version": "3.0.2", 4 | "description": "Create Error classes", 5 | "license": "MIT", 6 | "repository": "floatdrop/create-error-class", 7 | "author": { 8 | "name": "Vsevolod Strukchinsky", 9 | "email": "floatdrop@gmail.com", 10 | "url": "github.com/floatdrop" 11 | }, 12 | "engines": { 13 | "node": ">=0.10.0" 14 | }, 15 | "scripts": { 16 | "test": "mocha" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "" 23 | ], 24 | "dependencies": { 25 | "capture-stack-trace": "^1.0.0" 26 | }, 27 | "devDependencies": { 28 | "mocha": "*" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # create-error-class [![Build Status](https://travis-ci.org/floatdrop/create-error-class.svg?branch=master)](https://travis-ci.org/floatdrop/create-error-class) 2 | 3 | > Create error class 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install --save create-error-class 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | var createErrorClass = require('create-error-class'); 17 | 18 | var HTTPError = createErrorClass('HTTPError', function (props) { 19 | this.message = 'Status code is ' + props.statusCode; 20 | }); 21 | 22 | throw new HTTPError({statusCode: 404}); 23 | ``` 24 | 25 | 26 | ## API 27 | 28 | ### createErrorClass(className, [setup]) 29 | 30 | Return constructor of Errors with `className`. 31 | 32 | #### className 33 | 34 | *Required* 35 | Type: `string` 36 | 37 | Class name of Error Object. Should contain characters from `[0-9a-zA-Z_$]` range. 38 | 39 | #### setup 40 | Type: `function` 41 | 42 | Setup function, that will be called after each Error object is created from constructor with context of Error object. 43 | 44 | By default `setup` function sets `this.message` as first argument: 45 | 46 | ```js 47 | var MyError = createErrorClass('MyError'); 48 | 49 | new MyError('Something gone wrong!').message; // => 'Something gone wrong!' 50 | ``` 51 | 52 | ## License 53 | 54 | MIT © [Vsevolod Strukchinsky](http://github.com/floatdrop) 55 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /* global it */ 2 | 3 | 'use strict'; 4 | 5 | var assert = require('assert'); 6 | var createErrorClass = require('./'); 7 | 8 | var CustomError = createErrorClass('CustomError', function (message) { 9 | this.message = message; 10 | }); 11 | 12 | it('should work', function () { 13 | function doSomethingBad() { 14 | throw new CustomError('It went bad!'); 15 | } 16 | 17 | try { 18 | doSomethingBad(); 19 | } catch (err) { 20 | assert(err.name === 'CustomError', 'The name property should be set to the errors name'); 21 | assert(err instanceof CustomError, 'The error should be an instance of its class'); 22 | assert(err instanceof Error, 'The error should be an instance of builtin Error'); 23 | assert(require('util').isError(err), 'The error should be recognized by Node.js util#isError'); 24 | assert(err.stack, 'The error should have recorded a stack'); 25 | assert.strictEqual(err.toString(), 'CustomError: It went bad!', 'toString should return the default error message formatting'); 26 | assert.strictEqual(err.stack.split('\n')[0], 'CustomError: It went bad!', 'The stack should start with the default error message formatting'); 27 | assert.strictEqual(err.stack.split('\n')[1].indexOf('doSomethingBad'), 7, 'The first stack frame should be the function where the error was thrown.'); 28 | } 29 | }); 30 | 31 | it('should throw on invalid className', function () { 32 | assert.throws(function () { 33 | createErrorClass('something(){}); console.error("Run, you fools!"); (function myError'); 34 | }, /className contains invalid characters/); 35 | }); 36 | 37 | it('should capture a message if setup is not defined', function () { 38 | var NoSetupError = createErrorClass('NoSetupError'); 39 | 40 | try { 41 | throw new NoSetupError('Oh noes!'); 42 | } catch (err) { 43 | assert.strictEqual(err.message, 'Oh noes!'); 44 | } 45 | }); 46 | --------------------------------------------------------------------------------