Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made callback truly optional; made ERR() return the err #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions ERR.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,12 @@ module.exports = function (err, callback)
err = new Error(err);
}

//there is a callback, lets call it
//if a callback is passed, lets call it
if(callback != null)
{
callback(err);
}
//no callback, throw the error
else
{
throw err;
}
}

//return true if an error happend
return err != null;
return err;
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,6 @@ var ERR = require("async-stacktrace");
The parameters of `ERR()` are:

1. `err` The error object (can be a string that describes the error too)
2. `callback` (optional) If the callback is set and an error is passed, it will call the callback with the modified stacktrace. Else it will throw the error
2. `callback` (optional) If the callback is set and an error is passed, it will call the callback with the modified stacktrace. Otherwise it will do nothing (the caller can pass the returned `err` object to the callback if desired).

The return value is true if there is an error. Else its false
The return value is the `err` object passed as the first parameter (if it was passed as a string, it will be returned as a first-class `Error()` object).
22 changes: 7 additions & 15 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ vows.describe('ERR function').addBatch({
}
}).addBatch({
'when you call it without an error' : {
'it returns false' : function() {
assert.isFalse(ERR());
'it returns falsy' : function() {
assert(!ERR());
}
},
'when you call it with an error' : {
'it returns true' : function() {
assert.isTrue(ERR(new Error(), emptyFunc));
'it returns truthy' : function() {
assert(ERR(new Error(), emptyFunc));
}
},
'when you give it a callback and an error': {
Expand All @@ -37,19 +37,11 @@ vows.describe('ERR function').addBatch({
}
},
'when you give it no callback and an error': {
'it throws the error' : function() {
'it returns the error (so it can be passed to the callback)' : function() {
var wasThrown = false;

try
{
ERR(new Error());
}
catch(e)
{
wasThrown = true;
}

assert.isTrue(wasThrown);
var err = new Error();
assert.equal(ERR(err), err);
}
},
'when you call it with a string as an error' : {
Expand Down