Code Monkey home page Code Monkey logo

jesusgraterol / error-message-utils Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 229 KB

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.

Home Page: https://www.npmjs.com/package/error-message-utils

License: MIT License

TypeScript 100.00%
decoder encoder error error-handling error-management standard-error stderr utilities utils error-decoder

error-message-utils's Introduction

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!'

Identifying encoded errors:

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

isEncodedError('Some random unencoded error');
// false

isEncodedError(new Error('Some random unencoded error'));
// false

isEncodedError(encodeError('Some unknown error.', 'NASTY_ERROR'));
// true

isEncodedError(encodeError(new Error('Some unknown error.'), 'NASTY_ERROR'));
// true

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

error-message-utils's People

Contributors

jesusgraterol avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.