node.jsnodegit

Code fails to output errors or exceptions in Node.JS


I am using a Node.JS library called GitNode. I don't think it's important but basically it's an API for git commands in Node.JS.

I have a piece of code whose purpose is to create a new branch in an existing repository. After running the code, I know it's not creating a repository, and I also know it's not working correctly.

This is the entirety of my code:

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);

I've placed a lot of catch statements in the hopes of finding my error. But I can't seem to output anything. Neither the phrase "Bad_error" or "Bad_catch" output.

I know the code is broken and that createBranch isn't working. I tested some code that was supposed to run after I called repository.createBranch, but it never runs where as anything before createBranch is called will run. This indicates that CreateBranch is the problem.

So why are my errors not being caught?

Edit

I've fixed the code and it outputs the error (see answer below), but I'm still wondering why my other exceptions/error statements failed to work.


Solution

  • So, I figured out part of the problem thanks to here

    I just had to add a catch statement to my Repository.open() function.

    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
    

    If anyone is curious, turns out I was passing in the variable respository instead of repository :p

    And this will output bad. So that's good. I'm still not quite why this worked but my other catch statement didn't. I'm still looking for a good answer or explanation.