1.3.2 • Published 6 years ago

error-ex v1.3.2

Weekly downloads
17,674,133
License
MIT
Repository
github
Last release
6 years ago

node-error-ex Travis-CI.org Build Status Coveralls.io Coverage Rating

Easily subclass and customize new Error types

Examples

To include in your project:

var errorEx = require('error-ex');

To create an error message type with a specific name (note, that ErrorFn.name will not reflect this):

var JSONError = errorEx('JSONError');

var err = new JSONError('error');
err.name; //-> JSONError
throw err; //-> JSONError: error

To add a stack line:

var JSONError = errorEx('JSONError', {fileName: errorEx.line('in %s')});

var err = new JSONError('error')
err.fileName = '/a/b/c/foo.json';
throw err; //-> (line 2)-> in /a/b/c/foo.json

To append to the error message:

var JSONError = errorEx('JSONError', {fileName: errorEx.append('in %s')});

var err = new JSONError('error');
err.fileName = '/a/b/c/foo.json';
throw err; //-> JSONError: error in /a/b/c/foo.json

API

errorEx([name], [properties])

Creates a new ErrorEx error type

  • name: the name of the new type (appears in the error message upon throw; defaults to Error.name)
  • properties: if supplied, used as a key/value dictionary of properties to use when building up the stack message. Keys are property names that are looked up on the error message, and then passed to function values. - line: if specified and is a function, return value is added as a stack entry (error-ex will indent for you). Passed the property value given the key.
    • stack: if specified and is a function, passed the value of the property using the key, and the raw stack lines as a second argument. Takes no return value (but the stack can be modified directly).
    • message: if specified and is a function, return value is used as new .message value upon get. Passed the property value of the property named by key, and the existing message is passed as the second argument as an array of lines (suitable for multi-line messages).

Returns a constructor (Function) that can be used just like the regular Error constructor.

var errorEx = require('error-ex');

var BasicError = errorEx();

var NamedError = errorEx('NamedError');

// --

var AdvancedError = errorEx('AdvancedError', {
	foo: {
		line: function (value, stack) {
			if (value) {
				return 'bar ' + value;
			}
			return null;
		}
	}
}

var err = new AdvancedError('hello, world');
err.foo = 'baz';
throw err;

/*
	AdvancedError: hello, world
	    bar baz
	    at tryReadme() (readme.js:20:1)
*/

errorEx.line(str)

Creates a stack line using a delimiter

This is a helper function. It is to be used in lieu of writing a value object for properties values.

  • str: The string to create
    • Use the delimiter %s to specify where in the string the value should go
var errorEx = require('error-ex');

var FileError = errorEx('FileError', {fileName: errorEx.line('in %s')});

var err = new FileError('problem reading file');
err.fileName = '/a/b/c/d/foo.js';
throw err;

/*
	FileError: problem reading file
	    in /a/b/c/d/foo.js
	    at tryReadme() (readme.js:7:1)
*/

errorEx.append(str)

Appends to the error.message string

This is a helper function. It is to be used in lieu of writing a value object for properties values.

  • str: The string to append
    • Use the delimiter %s to specify where in the string the value should go
var errorEx = require('error-ex');

var SyntaxError = errorEx('SyntaxError', {fileName: errorEx.append('in %s')});

var err = new SyntaxError('improper indentation');
err.fileName = '/a/b/c/d/foo.js';
throw err;

/*
	SyntaxError: improper indentation in /a/b/c/d/foo.js
	    at tryReadme() (readme.js:7:1)
*/

License

Licensed under the MIT License. You can find a copy of it in LICENSE.

archetype-libraryeasy-select-rnchinjowwchinjowvuedragdropuploadimagesreact-native-bluetooth2killi8n-react-native-fast-imagepipihomern-send-sms@icanpm/api-master@arisageha/react-lazyload@arisageha/react-lazyload-fix@cashremit/cr-streamline-icons@almeidaa/msreact-native-template-rfbasecloud-archive-s3airscanairscan-examplebb-chatreact-native-esc-pos-sahaab@borisovart/atol-kkt-module@frxf/frxfsample-pratikpanchanideneme323112@steven-torres/jsxr@fundefund/funde_ck@ntt_app/react-native-custom-notificationreact-native-custom-text-hwjamesreact-native-covid-sdkgql_din_modbitgetafryxiconsreact-native-thanh-toast-librarymutasi-bca@jttechnic/interpreter@thanhnguyen14797/react-native-thanh-toast-library@eginnovations/eginnovations-capacitor-plugincthpb-plugin-social@olivervorasai/slidermysql-formatpanqibaoreact-native-printer-brothersrn-pdf-reader-offline@newhorizon-tech/dd-npm-package-templatereact-native-shekhar-bridge-testcogoportutilsukor-remasteruncoded-connectwilscanner@oiti/documentoscopy-react-native@respondea/cordova-plugin-v-inappbrowser@mink-opn/build-tokensquoc-testreact-native-slider-kfunblock-block-save-variables@infinitebrahmanuniverse/nolb-err@prodam/prodam-types@saaspe/componentsplginexpand-react-bridgeopea-bootstraapreact-typewritting-effectluminos-ui-coresklif-ui-kitsklif-api@everything-registry/sub-chunk-1595iqra-calculatorjawwy-sdkjawwy_gamification_releasereact-native-sphereuisphereuijawwy_libraryeginnovations-capacitor-pluginreact-native-credit-card-pkgp149-table@rabailriaz/hisaab-web-portalsklif-uireact-native-jawwy_samplelapture-ui-complaptureuigriffin-ui-librarynebula-http-resiliencycomposize@wacoco/ytaman-baca-masyarakattailwind-vector-effectsyncbackbasessystems-apitable-react-qvc-muitailwind-templatetaglisttest-iki-mini-apptest-library-123test-haptik-libteadanteatest-npm-jjmessiwinx-form-winxwontbreakweb-elements-iconsweb-component-tester-bundle
1.3.2

6 years ago

1.3.1

8 years ago

1.3.0

9 years ago

1.2.0

9 years ago

1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.0

9 years ago