Commit 8f911102 authored by Remy Suen's avatar Remy Suen

Expose libgit2 error code to clients when a promise fails

When an API function of libgit2 fails within a promise, the promise is
rejected with a JavaScript Error object that wraps the recorded error
message in giterr_last(). However, the original non-zero return code of
the API function is not exposed to the caller of the NodeGit API. This
means that NodeGit clients cannot easily identify what the cause of an
error is outside of parsing the Error object's message. This is
an extremely volatile way of determining the cause as libgit2 may
choose to alter the wording of the message at any time.

To solve the problem above, the returned JavaScript Error object now
esposes the libgit2 error code via its `errno` property.
parent 3e065305
......@@ -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)
......
......@@ -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)
......
......@@ -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)
......
......@@ -155,8 +155,15 @@ void {{ cppClassName }}::{{ cppFunctionName }}Worker::HandleOKCallback() {
callback->Call(2, argv);
} else {
if (baton->error) {
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));
Local<v8::Value> argv[1] = {
Nan::Error(baton->error->message)
err
};
callback->Call(1, argv);
if (baton->error->message)
......
......@@ -1298,4 +1298,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);
});
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment