Commit 63f2dac2 authored by Remy Suen's avatar Remy Suen

Expose JavaScript function name in thrown error

When an error happens in libgit2, NodeGit will wrap the and expose
the error code and message to be consumed via the JavaScript API.
However, this information is often insufficient given the
asynchronous nature of NodeGit and it may at times be unclear as to
which JavaScript function caused the error on the libgit2 side. By
exposing an 'errorFunction' property string that includes the name of
the JavaScript function in the error, debugging should now be a lot
easier as clients will now be able to quickly identify which
JavaScript function in a long promise chain was the source of an
error.
Signed-off-by: default avatarRemy Suen <remy.suen@gmail.com>
parent 6ff67a88
......@@ -315,6 +315,7 @@ var Helpers = {
fnDef.cppFunctionName = Helpers.cTypeToCppName(key, "git_" + typeDef.typeName);
fnDef.jsFunctionName = Helpers.cTypeToJsName(key, "git_" + typeDef.typeName);
fnDef.jsClassName = typeDef.jsClassName;
if (fnDef.cppFunctionName == typeDef.cppClassName) {
fnDef.cppFunctionName = fnDef.cppFunctionName.replace("Git", "");
......
......@@ -102,6 +102,7 @@ void GitPatch::ConvenientFromDiffWorker::HandleOKCallback() {
err = Nan::Error("Method convenientFromDiff has thrown an error.")->ToObject();
}
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("Patch.convenientFromDiff").ToLocalChecked());
Local<v8::Value> argv[1] = {
err
};
......@@ -119,6 +120,7 @@ void GitPatch::ConvenientFromDiffWorker::HandleOKCallback() {
if (baton->error_code < 0) {
Local<v8::Object> err = Nan::Error("method convenientFromDiff has thrown an error.")->ToObject();
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("Patch.convenientFromDiff").ToLocalChecked());
Local<v8::Value> argv[1] = {
err
};
......
......@@ -87,6 +87,7 @@ void GitRemote::ReferenceListWorker::HandleOKCallback()
{
Local<v8::Object> err = Nan::Error("Reference List has thrown an error.")->ToObject();
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("Remote.referenceList").ToLocalChecked());
Local<v8::Value> argv[1] = {
err
};
......
......@@ -95,6 +95,7 @@ void GitRevwalk::FastWalkWorker::HandleOKCallback()
err = Nan::Error("Method fastWalk has thrown an error.")->ToObject();
}
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("Revwalk.fastWalk").ToLocalChecked());
Local<v8::Value> argv[1] = {
err
};
......@@ -159,6 +160,7 @@ void GitRevwalk::FastWalkWorker::HandleOKCallback()
{
Local<v8::Object> err = Nan::Error("Method next has thrown an error.")->ToObject();
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("Revwalk.fastWalk").ToLocalChecked());
Local<v8::Value> argv[1] = {
err
};
......
......@@ -296,6 +296,7 @@ void GitRevwalk::FileHistoryWalkWorker::HandleOKCallback()
err = Nan::Error("Method fileHistoryWalk has thrown an error.")->ToObject();
}
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("Revwalk.fileHistoryWalk").ToLocalChecked());
Local<v8::Value> argv[1] = {
err
};
......@@ -312,6 +313,7 @@ void GitRevwalk::FileHistoryWalkWorker::HandleOKCallback()
if (baton->error_code < 0) {
Local<v8::Object> err = Nan::Error("Method next has thrown an error.")->ToObject();
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("Revwalk.fileHistoryWalk").ToLocalChecked());
Local<v8::Value> argv[1] = {
err
};
......
......@@ -107,6 +107,7 @@ void GitFilterRegistry::RegisterWorker::HandleOKCallback() {
err = Nan::Error("Method register has thrown an error.")->ToObject();
}
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("FilterRegistry.register").ToLocalChecked());
v8::Local<v8::Value> argv[1] = {
err
};
......@@ -118,6 +119,7 @@ void GitFilterRegistry::RegisterWorker::HandleOKCallback() {
else if (baton->error_code < 0) {
v8::Local<v8::Object> err = Nan::Error("Method register has thrown an error.")->ToObject();
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("FilterRegistry.register").ToLocalChecked());
v8::Local<v8::Value> argv[1] = {
err
};
......@@ -191,6 +193,7 @@ void GitFilterRegistry::UnregisterWorker::HandleOKCallback() {
err = Nan::Error("Method register has thrown an error.")->ToObject();
}
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("FilterRegistry.unregister").ToLocalChecked());
v8::Local<v8::Value> argv[1] = {
err
};
......@@ -202,6 +205,7 @@ void GitFilterRegistry::UnregisterWorker::HandleOKCallback() {
else if (baton->error_code < 0) {
v8::Local<v8::Object> err = Nan::Error("Method unregister has thrown an error.")->ToObject();
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("FilterRegistry.unregister").ToLocalChecked());
v8::Local<v8::Value> argv[1] = {
err
};
......
......@@ -161,6 +161,7 @@ void {{ cppClassName }}::{{ cppFunctionName }}Worker::HandleOKCallback() {
err = Nan::Error("Method {{ jsFunctionName }} has thrown an error.")->ToObject();
}
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("{{ jsClassName }}.{{ jsFunctionName }}").ToLocalChecked());
v8::Local<v8::Value> argv[1] = {
err
};
......@@ -222,6 +223,7 @@ void {{ cppClassName }}::{{ cppFunctionName }}Worker::HandleOKCallback() {
if (!callbackFired) {
v8::Local<v8::Object> err = Nan::Error("Method {{ jsFunctionName }} has thrown an error.")->ToObject();
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("{{ jsClassName }}.{{ jsFunctionName }}").ToLocalChecked());
v8::Local<v8::Value> argv[1] = {
err
};
......
......@@ -1628,6 +1628,7 @@ describe("Merge", function() {
"should not be able to retrieve common merge base"));
}, function(err) {
assert.equal("no merge base found", err.message);
assert.equal("Merge.base", err.errorFunction);
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