error-message-utils
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Error Message Utils

The error-message-utils package simplifies error management in your web applications and RESTful APIs. It ensures consistent and scalable handling of error messages, saving you time and effort. Moreover, it gives you the ability to assign custom error codes so all possible cases can be handled accordingly.

Getting Started

Install the package:

$ npm install -S error-message-utils

Usage

Encoding an error:

import { encodeError } from 'error-message-utils';

if (emailExists()) {
  throw new Error(encodeError(
    'The provided email is already in use.', 
    'EMAIL_EXISTS'
  ));
  // 'The provided email is already in use.{(EMAIL_EXISTS)}'
}

Decoding an error:

import { decodeError } from 'error-message-utils';

decodeError('The provided email is already in use.{(EMAIL_EXISTS)}');
// {
//   message: 'The provided email is already in use.',
//   code: 'EMAIL_EXISTS'
// }

Error messages can be extracted recursively from complex structures, including nested cause data properties from Error instances:

import { extractMessage } from 'error-message-utils';

extractMessage(new Error('Top level error', {
  cause: new Error('First nested cause', {
    cause: new Error('Second nested cause'),
  }),
}));
// 'Top level error; [CAUSE]: First nested cause; [CAUSE]: Second nested cause'


extractMessage({ 
  message: { 
    err: { 
      message: 'This error message is nested deeply!'
    } 
  } 
});
// 'This error message is nested deeply!'

Built With

  • TypeScript

Running the Tests

$ npm run test:unit

License

MIT


Acknowledgments

  • ...

@TODOS

  • [ ] ...

Deployment

Install dependencies:

$ npm install

Build the library:

$ npm start

Publish to npm:

$ npm publish

Package Sidebar

Install

npm i error-message-utils

Weekly Downloads

85

Version

1.0.2

License

MIT

Unpacked Size

9.21 kB

Total Files

17

Last publish

Collaborators

  • jesusgraterol