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

Model.count 的时候,在 callback 函数里面 throw Error 会触发两次 callback #34

Closed
plusmancn opened this Issue Jan 19, 2016 · 1 comment

Comments

Projects
None yet
2 participants
@plusmancn
Copy link

plusmancn commented Jan 19, 2016

Foo.count(function(err, count) {
    console.log(count);
    throw Error('...');
});

这段代码的 callback 会被多次触发。

@XadillaX XadillaX changed the title Query.prototype.count 方法,try{}catch(){} 误截获应用错误问题 Model.count 的时候,在 callback 函数里面 throw Error 会触发两次 callback Jan 19, 2016

@XadillaX

This comment has been minimized.

Copy link
Owner

XadillaX commented Jan 19, 2016

问题在于这里

Toshihiko/lib/query.js

Lines 717 to 723 in 50376fa

try {
callback(undefined, result[0]["COUNT(0)"]);
promisor._onExecuteResult(undefined, result[0]["COUNT(0)"]);
} catch(e) {
callback(undefined, 0);
promisor._onExecuteResult(undefined, 0);
}

try {
    callback(undefined, result[0]["COUNT(0)"]);
    promisor._onExecuteResult(undefined, result[0]["COUNT(0)"]);
} catch(e) {
    callback(undefined, 0);
    promisor._onExecuteResult(undefined, 0);
}

count 的回调函数里面 throw Error 的时候,会被 count 函数内部本身为了获取 result[0]["COUNT(0)"] 错误的 try catch 捕获到,于是下面再次执行了一遍 callback

XadillaX added a commit that referenced this issue Jan 19, 2016

@XadillaX XadillaX closed this in e567060 Jan 19, 2016

XadillaX added a commit that referenced this issue Jan 19, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment