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

fix: should not throw error when pass null #99

Merged
merged 1 commit into from Feb 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/ready.js
Expand Up @@ -80,6 +80,9 @@ class Ready extends EventEmitter {
opt.name = name || cacheKey;
const timer = setTimeout(() => this.emit('ready_timeout', opt.name), opt.timeout);
const cb = once(err => {
if (err != null && !(err instanceof Error)) {
err = new Error(err);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

improve the stack

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once err is undefined, this will be broken.

Should be err !== null && err !== undefined instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err != null is equal to err !== null && err !== undefined

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yes. I got wrong.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about moving this logic to readyDone? So you can deal with error together at one place.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the stack, so this way is better rather than after setImmdiate

}
clearTimeout(timer);
// won't continue to fire after it's error
if (this.isError === true) return;
Expand All @@ -102,11 +105,8 @@ class Ready extends EventEmitter {
* @return {Ready} this
*/
readyDone(id, opt, err) {
if (err !== undefined && !opt.isWeakDep) {
if (err != null && !opt.isWeakDep) {
this.isError = true;
if (!(err instanceof Error)) {
err = new Error(err);
}
debug('[%s] Throw error task id `%s`, error %s', id, opt.name, err);
return this.emit('error', err);
}
Expand Down
3 changes: 3 additions & 0 deletions test/ready.test.js
Expand Up @@ -406,6 +406,9 @@ describe('Ready', function() {
err = yield assertErrorType(true);
assert(err.message === 'true');

err = yield assertErrorType(null);
assert(err === undefined);

function assertErrorType(value) {
const obj = {};
const ready = new Ready();
Expand Down