throw

A simple utility that helps throw exceptions.

  • throw

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
throw
701.1.12 years ago8 years agoMinified + gzip package size for throw in KB

Readme

throw
You can't use throw statement in expressions in JavaScript:
arg = arg || throw new Error('arg is required');
// => SyntaxError: Unexpected token throw

This tiny library is wrapping throw in a function:
var thr = require('throw');

// ...

arg = arg || thr('arg is required');

Messages can contain printf-like placeholders:
arg = arg || thr('"%s" is required', argName);

You can specify instance of Error instead of message:
arg = arg || thr(new Error('arg is required'));

Or parametrize error class:
arg = arg || thr(Error, 'arg is required');

Installation

$ npm install --save throw

Usage

var thr = require('throw');

var parsed = parse(str) || thr('Could not parse "%s"', str);

License

MIT