Skip to content

Commit

Permalink
Merge pull request #1209 from rcjsuen/errno
Browse files Browse the repository at this point in the history
Expose libgit2 error code to clients when a promise fails
  • Loading branch information
johnhaley81 committed Feb 28, 2017
2 parents 6b1f73b + b40ee03 commit e66d20c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
9 changes: 8 additions & 1 deletion generate/templates/manual/patches/convenient_patches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,15 @@ void GitPatch::ConvenientFromDiffWorker::HandleOKCallback() {
}

if (baton->error) {
Local<v8::Object> err;
if (baton->error->message) {
err = Nan::Error(baton->error->message)->ToObject();
} else {
err = Nan::Error("Method convenientFromDiff has thrown an error.")->ToObject();
}
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
Local<v8::Value> argv[1] = {
Nan::Error(baton->error->message)
err
};
callback->Call(1, argv);
if (baton->error->message)
Expand Down
9 changes: 8 additions & 1 deletion generate/templates/manual/revwalk/fast_walk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,15 @@ void GitRevwalk::FastWalkWorker::HandleOKCallback()
{
if (baton->error)
{
Local<v8::Object> err;
if (baton->error->message) {
err = Nan::Error(baton->error->message)->ToObject();
} else {
err = Nan::Error("Method fastWalk has thrown an error.")->ToObject();
}
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
Local<v8::Value> argv[1] = {
Nan::Error(baton->error->message)
err
};
callback->Call(1, argv);
if (baton->error->message)
Expand Down
9 changes: 8 additions & 1 deletion generate/templates/manual/revwalk/file_history_walk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,15 @@ void GitRevwalk::FileHistoryWalkWorker::HandleOKCallback()
}

if (baton->error) {
Local<v8::Object> err;
if (baton->error->message) {
err = Nan::Error(baton->error->message)->ToObject();
} else {
err = Nan::Error("Method fileHistoryWalk has thrown an error.")->ToObject();
}
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
Local<v8::Value> argv[1] = {
Nan::Error(baton->error->message)
err
};
callback->Call(1, argv);
if (baton->error->message)
Expand Down
9 changes: 8 additions & 1 deletion generate/templates/partials/async_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,15 @@ void {{ cppClassName }}::{{ cppFunctionName }}Worker::HandleOKCallback() {
callback->Call(2, argv);
} else {
if (baton->error) {
v8::Local<v8::Object> err;
if (baton->error->message) {
err = Nan::Error(baton->error->message)->ToObject();
} else {
err = Nan::Error("Method {{ jsFunctionName }} has thrown an error.")->ToObject();
}
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
v8::Local<v8::Value> argv[1] = {
Nan::Error(baton->error->message)
err
};
callback->Call(1, argv);
if (baton->error->message)
Expand Down
22 changes: 22 additions & 0 deletions test/tests/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -1612,4 +1612,26 @@ describe("Merge", function() {
assert.ok(repository.isDefaultState());
});
});

it("can retrieve error code on if common merge base not found", function() {
var repo;
return NodeGit.Repository.open(local("../repos/workdir"))
.then(function(r) {
repo = r;
return repo.getCommit("4bd806114ce26503c103c85dcc985021951bbc18");
})
.then(function(commit) {
return commit.getParents(commit.parentcount());
})
.then(function(parents) {
return NodeGit.Merge.base(repo, parents[0], parents[1])
.then(function() {
return Promise.reject(new Error(
"should not be able to retrieve common merge base"));
}, function(err) {
assert.equal("no merge base found", err.message);
assert.equal(NodeGit.Error.CODE.ENOTFOUND, err.errno);
});
});
});
});

0 comments on commit e66d20c

Please sign in to comment.