0.1.0 • Published 6 years ago

aug-error v0.1.0

Weekly downloads
2
License
GPL-3.0
Repository
github
Last release
6 years ago

aug-error

Augmented errors

Set up

npm install --save aug-error # for npm
yarn add aug-error # for yarn

Usage

const path = require('path');
const augError = require('aug-error');

augError.registerMessagesFromDirSync(path.resolve(__dirname, 'errors'));

doSomething((err) => {
  if (err) {
    throw augError.augment(new Error(), augError.codes.SOME_CODE, {
      somethingWentWrong: 'details about what went wrong',
    }, err);
  }
});

augError.augment(err: Error, code: string, details?: Object, causedBy?: Error)

Returns an Error object, that has been augmented with code, causedBy, and details. In fact the passed in empty Error object is returned, having simply been augmented with this. The reason for passing in the Error object rather than having aug-error generate one internally, is so that the stacktrace of the error is more meaningful - that is, remains unpolluted by any part of the aug-error library, and entirely part of the context from within it is called.

augError.registerMessagesFromDirSync(dir: string, langs?: string[])

Loads errors in different languages from the specified dir. A default.json is always loaded, and any additional languages specified in langs will be loaded too. The contents of these files are expected to be simple flat objects where the keys are the error codes, and the values are the display messages. All the language files should have a matching set of error codes.

Author

Brendan Graetz

Licence

GPL-3.0

0.1.0

6 years ago

0.0.0

6 years ago