Skip to content

Commit

Permalink
added async error throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed May 23, 2014
1 parent b18bc9e commit 0d92380
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 23 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,35 @@ will be concatenated according to rules

There will be single space between the individual parts.

## Lazy async assertions

Sometimes you do not want to throw an error synchronously, breaking the entire
execution stack. Instead you can throw an error asynchronously using `lazyAssync`,
which internally implements logic like this:

```js
if (!condition) {
setTimeout(function () {
throw new Error('Conditions is false!');
}, 0);
}
```

This allows the execution to continue, while your global error handler (like
my favorite [Sentry](http://bahmutov.calepin.co/know-unknown-unknowns-with-sentry.html))
can still forward the error with all specified information to your server.

```js
lazyAssync(false, 'foo');
console.log('after assync');
// output
after assync
Uncaught Error: foo
```

In this case, there is no meaningful error stack, so use good message
arguments - there is no performance penalty!

### Small print

Author: Gleb Bahmutov © 2014
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lazy-ass",
"main": "index.js",
"version": "0.1.2",
"version": "0.2.0",
"homepage": "https://github.com/bahmutov/lazy-ass",
"license": "MIT",
"ignore": [
Expand Down
54 changes: 34 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,52 @@
(function initLazyAss() {

var lazyAss = function (condition) {
var args = [].slice.call(arguments, 1);
function formMessage(args) {
var msg = args.reduce(function (total, arg, k) {
if (k) {
total += ' ';
}
if (typeof arg === 'string') {
return total + arg;
}
if (typeof arg === 'function') {
return total + arg();
}
if (Array.isArray(arg)) {
return total + JSON.stringify(arg);
}
return total + JSON.stringify(arg, null, 2);
}, '');
return msg;
}

var lazyAss = function lazyAss(condition) {
if (!condition) {
var args = [].slice.call(arguments, 1);
throw new Error(formMessage(args));
}
};

var lazyAssync = function lazyAssync(condition) {
if (!condition) {
var msg = args.reduce(function (total, arg, k) {
if (k) {
total += ' ';
}
if (typeof arg === 'string') {
return total + arg;
}
if (typeof arg === 'function') {
return total + arg();
}
if (Array.isArray(arg)) {
return total + JSON.stringify(arg);
}
return total + JSON.stringify(arg, null, 2);
}, '');
throw new Error(msg);
var args = [].slice.call(arguments, 1);
setTimeout(function () {
throw new Error(formMessage(args));
}, 0);
}
};

function register(value, name) {
if (typeof window === 'object') {
/* global window */
window[name] = value;
} else if (typeof module === 'object') {
module.exports = value;
} else if (typeof global === 'object') {
global[name] = value;
} else {
throw new Error('Do not know how to register ' + name);
}
}

register(lazyAss, 'lazyAss');
register(lazyAssync, 'lazyAssync');

}());
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lazy-ass",
"description": "Lazy assertions without performance penalty",
"version": "0.1.2",
"version": "0.2.0",
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>",
"bugs": {
"url": "https://github.com/bahmutov/lazy-ass/issues"
Expand Down
6 changes: 5 additions & 1 deletion test/lazy-ass.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* global lazyAss */
if (typeof window === 'undefined') {
require('..'); // Node
}
if (typeof lazyAss === 'undefined') {
var lazyAss = require('..');
throw new Error('Cannot find lazyAss global varible');
}
if (typeof expect === 'undefined') {
var expect = require('expect.js');
Expand Down
32 changes: 32 additions & 0 deletions test/lazy-assync.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* global lazyAssync */
if (typeof window === 'undefined') {
require('..'); // Node
}
if (typeof lazyAssync === 'undefined') {
throw new Error('Cannot find lazyAssync global varible');
}
if (typeof expect === 'undefined') {
var expect = require('expect.js');
}

describe('lazyAssync', function () {
describe('lazyAssync function itself', function () {
it('is a function', function () {
expect(lazyAssync).not.to.be(undefined);
expect(lazyAssync).to.be.a('function');
});

it('does not throw if condition is true', function () {
expect(function () {
lazyAssync(true);
}).not.to.throwError();
});

it('does not evaluate function if condition is true', function () {
function foo() {
throw new Error('Foo has been called');
}
lazyAssync(true, foo);
});
});
});

0 comments on commit 0d92380

Please sign in to comment.