Skip to content
This repository was archived by the owner on Dec 2, 2026. It is now read-only.
This repository was archived by the owner on Dec 2, 2026. It is now read-only.

Provide error code when a function call has failed #1206

Closed

Description

@rcjsuen
return git.Merge.base(repo, parents[0], parents[1]);
.then(function(ancestor) {
  console.log(ancestor);
})
.catch(function(err) {
  console.log(err);
})

Running the code above will print out the output below if the two commits have no common ancestor.

[Error: No merge base found]

The error code behind this error is Error.CODE.ENOTFOUND but there is no way for a client to know this. Not having a common ancestor between two commits is an error that a client may be able to handle gracefully but if it the function failed because of some other fatal error then maybe the client can't do anything about it. Without an error code available in the catch callback, clients have no way of figuring out why the function failed outside of parsing the error message itself. It would be nice if we could have something like either of the two following patterns...

catch(function(err, code) {
  console.log(err);
  console.log(code);
});
catch(function(err) {
  console.log(err.msg);
  console.log(err.code);
});

Activity

  1. rcjsuen commented on Jan 30, 2017

    @rcjsuen
    MemberAuthor

    I couldn't figure out how to implement the first pattern with two arguments since the promise didn't seem to want to return two things. :(

    I did get some code working that implements the second pattern though. I changed the error code's property name to be errno instead of code to match what was already used elsewhere in async_function.cc.

    Are either of the following two proposed solutions acceptable?
    If not, can anyone suggest an alternative solution that would be acceptable? Thanks!

    // err
    { [Error: No merge base found] errno: -3 }
    // err.message
    [Error: No merge base found]
    // err.errno
    -3
    // err
    { msg: [Error: No merge base found], errno: -3 }
    // err.msg
    [Error: No merge base found]
    // err.errno
    -3
  2. maxkorp commented on Jan 30, 2017

    @maxkorp
    Collaborator

    I like the second proposal for sure.
    CC: @johnhaley81 @tbranyen Any thoughts?

  3. tbranyen commented on Jan 30, 2017

    @tbranyen
    Member

    In order to pass two values to a then you'd have to resolve an array and then use destructuring:

    catch(function([ err, code ]) {
      console.log(err);
      console.log(code);
    });

    That said, I much prefer the second approach, except why is there msg? Why can't you just attach the errno directly to the error object?

  4. rcjsuen commented on Jan 30, 2017

    @rcjsuen
    MemberAuthor

    Why can't you just attach the errno directly to the error object?

    We can do that, @tbranyen. That is what is shown in the first example of my second comment.

  5. rcjsuen commented on Jan 31, 2017

    @rcjsuen
    MemberAuthor

    I have pushed PR #1209 to expose the error code to NodeGit clients.

  6. tbranyen commented on Feb 10, 2017

    @tbranyen
    Member

    Just wanted to leave this internal Node error code reference: https://github.com/nodejs/node/blob/master/doc/guides/using-internal-errors.md

  7. rcjsuen commented on Feb 28, 2017

    @rcjsuen
    MemberAuthor

    Closing as PR #1209 has been merged in. Will be included in 0.18.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions