Skip to content

Commit 82cf59e

Browse files
implausiblejohnhaley81
authored andcommitted
reject if async function returns < 0
If the function that our async worker calls returns a value less than 0, then it is an error and should be treated as an error. Even if the error message is lost, it is necessary to return a general error, or otherwise systems that would otherwise reject, will resolve.
1 parent d6916d5 commit 82cf59e

2 files changed

Lines changed: 42 additions & 9 deletions

File tree

generate/templates/partials/async_function.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ void {{ cppClassName }}::{{ cppFunctionName }}Worker::HandleOKCallback() {
139139
if (baton->error->message)
140140
free((void *)baton->error->message);
141141
free((void *)baton->error);
142+
} else if (baton->error_code < 0) {
143+
Local<v8::Value> argv[1] = {
144+
Nan::Error("A general error has occurred")
145+
};
146+
callback->Call(1, argv);
142147
} else {
143148
callback->Call(0, NULL);
144149
}

test/tests/remote.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,40 @@ describe("Remote", function() {
241241
});
242242
});
243243

244+
it("will reject if credentials promise rejects", function() {
245+
this.timeout(5000);
246+
var repo = this.repository;
247+
var branch = "should-not-exist";
248+
return Remote.lookup(repo, "origin")
249+
.then(function(remote) {
250+
var ref = "refs/heads/" + branch;
251+
var refs = [ref + ":" + ref];
252+
var options = {
253+
callbacks: {
254+
credentials: function(url, userName) {
255+
return Promise.reject();
256+
},
257+
certificateCheck: function() {
258+
return 1;
259+
}
260+
}
261+
};
262+
return remote.push(refs, options);
263+
})
264+
// takes care of windows bug, see the .catch for the proper pathway
265+
// that this flow should take (cred cb doesn't run twice -> throws error)
266+
.then(function() {
267+
return Promise.reject(
268+
new Error("should not be able to push to the repository"));
269+
}, function(err) {
270+
if (err.message === "A general error has occurred") {
271+
return Promise.resolve();
272+
} else {
273+
throw err;
274+
}
275+
});
276+
});
277+
244278
it("cannot push to a repository with invalid credentials", function() {
245279
this.timeout(5000);
246280
var repo = this.repository;
@@ -249,19 +283,13 @@ describe("Remote", function() {
249283
.then(function(remote) {
250284
var ref = "refs/heads/" + branch;
251285
var refs = [ref + ":" + ref];
252-
var firstPass = true;
253286
var options = {
254287
callbacks: {
255288
credentials: function(url, userName) {
256-
if (firstPass) {
257-
firstPass = false;
258-
if (url.indexOf("https") === -1) {
259-
return NodeGit.Cred.sshKeyFromAgent(userName);
260-
} else {
261-
return NodeGit.Cred.userpassPlaintextNew(userName, "");
262-
}
289+
if (url.indexOf("https") === -1) {
290+
return NodeGit.Cred.sshKeyFromAgent(userName);
263291
} else {
264-
return NodeGit.Cred.defaultNew();
292+
return NodeGit.Cred.userpassPlaintextNew(userName, "");
265293
}
266294
},
267295
certificateCheck: function() {

0 commit comments

Comments
 (0)