2017-05-22 25 views
0

我正在使用名爲GitNode的Node.JS庫。我不認爲這很重要,但基本上它是Node.JS中的git命令的API。代碼無法在Node.JS中輸出錯誤或異常

我有一段代碼,其目的是在現有的存儲庫中創建一個新的分支。運行代碼後,我知道它不是創建一個存儲庫,我也知道它不能正常工作。

這是我的代碼全部:

var Git = require("nodegit"); 

var getMostRecentCommit = function(repository) { 
    return repository.getBranchCommit("master"); 
}; 

var makeBranch = function(respository) { 
    console.log("in here1") 
    repository.createBranch("tester", getMostRecentCommit(repository)).then(
     function(reference) { 
      console.log("good")},    // if it works 
     function(reasonForFailure) { 
      console.log("bad_error")})   // createBranch has error case 
     .catch(function(reasonForFailure) { 
      console.log("bad_catch")});   // if the error function fails, I also have a catch statement 
}; 


Git.Repository.open("../test_repo") 
    .then(makeBranch); 

我已經放置了很多catch語句中找到我的錯誤的希望。但我似乎無法輸出任何內容。短語「Bad_error」或「Bad_catch」輸出。

我知道代碼已損壞,createBranch無法正常工作。我測試了一些在我調用repository.createBranch後應該運行的代碼,但它在調用createBranch之前無法運行。這表明CreateBranch是問題所在。

那麼爲什麼我的錯誤不被捕獲?

編輯

我已經固定的代碼,並將其輸出誤差(見下文答覆),但我仍然不知道爲什麼我的其他異常/錯誤陳述未能奏效。

回答

0

所以,我想通了這個問題感謝的一部分here

我不得不catch語句添加到我的Repository.open()函數。

var makeBranch = function(respository) { 
    console.log("in here1") 
    repository.createBranch("tester", getMostRecentCommit(repository)).then(
     function(reference) { 
      console.log("good")},    
     function(reasonForFailure) { 
      console.log("bad_error")})   
     .catch(function(reasonForFailure) { 
      console.log("bad_catch")});   
}; 


Git.Repository.open("../test_repo") 
    .then(makeBranch).catch(
    function(reasonForFailure) {console.log("bad")}); // This Works 

如果有人好奇,原來我傳遞變量respository而不是repository:P

,這將輸出bad。這很好。我仍然不太明白爲什麼這個工作,但我的其他捕獲聲明沒有。我仍在尋找一個很好的答案或解釋。