JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 49194
  • Score
    100M100P100Q149071F
  • License MIT

It helps to know why you got an error.

Package Exports

  • yerror

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (yerror) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

YError

Better errors for your NodeJS code.

NPM version Build status Dependency Status devDependency Status Coverage Status Code Climate

Usage

First, require me where you could throw errors:

var YError = require('yerror');

Then, emit errors with a bonus: parameters!

function doSomething(pay, action) {
  if(parseInt(pay, 10) !== pay) {
    throw new YError('E_BAD_PAY', pay, action);
  }
}

doSomething('nuts', 'code');


// YError: E_BAD_PAY (nuts, code)
//   at doSomething (/home/nfroidure/simplifield/yerror/test.js:5:11)
//   at Object.<anonymous> (/home/nfroidure/simplifield/yerror/test.js:9:1)
//   (...)

You don't have to use constant like error messages, we use this convention mainly for i18n reasons.

Also, you could want to wrap errors and keep a valuable stack trace:

function doSomethingAsync(pay, action) {
  return  new Promise(function(resolve, reject) {
    try {
      doSomething(pay, action);
      resolve();
    } catch(err) {
      reject(YError.bump(err));
    }
  });
}

doSomethingAsync('nuts', 'code')
  .catch(function(err) {
    console.log(err.stack);
  });

// YError: E_BAD_PAY (nuts, code)
//    at doSomething (/home/nfroidure/simplifield/yerror/test.js:5:11)
//    (...)
// YError: E_BAD_TRANSACTION (pay)
//    at Function.YError.wrap (/home/nfroidure/simplifield/yerror/src/index.js:41:12)
//    at /home/nfroidure/simplifield/yerror/test.js:16:21
//    at doSomethingAsync (/home/nfroidure/simplifield/yerror/test.js:11:11)
//    (...)

API

YError(msg:String, ...args:Mixed):Error

Creates a new YError with msg as a message and args as debug values.

YError.wrap(err:Error, msg:String, ...args:Mixed):Error

Wraps any error and output a YError with msg as its message and args as debug values.

YError.cast(err:Error, msg:String, ...args:Mixed):Error

Return YError as is or wraps any other error and output a YError with msg as its message and args as debug values.

YError.bump(err:Error, fallbackMsg:String, ...fallbackArgs:Mixed):Error

Same than YError.wrap() but preserves the message and the debug values of the YError errors.

Stats

NPM NPM