Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include the command and args used to call it in the error object #194

Open
ysabri opened this issue Jun 4, 2018 · 2 comments
Open

Include the command and args used to call it in the error object #194

ysabri opened this issue Jun 4, 2018 · 2 comments
Milestone

Comments

@ysabri
Copy link
Contributor

ysabri commented Jun 4, 2018

This is in reference to my comment on #144.

I feel that the Git command + the args used to call the command should be included in the error object. I find it very useful sometimes when debugging to know what args were used to call the command that errored out, especially for common errors (#144 explains what a common error is).

For example if I'm iterating over a list of branches to get their logs and one of them errors out with a common error. The only way for me to find out which branch caused the error is to catch the error in the function and print out the branch name right there. This can get annoying since I can handle all errors in one place at a higher level if the information was included already.

Another thing I can think of, this can also be helpful in logging errors given that exec is asynchronous.

@shiftkey shiftkey added this to the 2.0 milestone Nov 20, 2018
@shiftkey
Copy link
Member

@ysabri i'm curious to explore this, but I'd love an example snippet to start from that's dugite focused.

GitProcess.exec will happily return you a failing Git operation (with the exit code set), so doing something like this doesn't feel that compelling, based on what you've provided:

const testRepoPath = temp.mkdirSync('desktop-git-test-blank')
const args = ['branch', 'branch-does-not-exist']

const result = await GitProcess.exec(args, testRepoPath)

expect(result.args).toEqual(args)
expect(result.path).toBe(testRepoPath)

But maybe we could generalize some of the error handling infrastructure in GitHub Desktop and pull that into dugite.

For example, we have this error type in GitHub Desktop:

export class GitError extends Error {
  /** The result from the failed command. */
  public readonly result: IGitResult

  /** The args for the failed command. */
  public readonly args: ReadonlyArray<string>

  public constructor(result: IGitResult, args: ReadonlyArray<string>) {
    super(getResultMessage(result))

    this.name = 'GitError'
    this.result = result
    this.args = args
  }
}

Which we use to throw if we aren't able to convert the error code into a meaningful app-specific error code: https://github.com/desktop/desktop/blob/1b69c2f04bd55afbe363821dccd0a3966c40e509/app/src/lib/git/core.ts#L166

But we also have a bunch of additional infrastructure about expected exit codes and you might not need the full experience here...

@ysabri
Copy link
Contributor Author

ysabri commented Dec 27, 2018

Yes, that is exactly what I was talking about. I like how you guys have the args in GitError type in GitHub Desktop.

In fact I really liked what you guys did in GitHub Desktop, so for my project I did something very similar but a little more general for my use case.

Keep in mind the code below is still work in progress :)
Here: https://github.com/ysabri/GitDrive/blob/master/src/git-drive/git/core-git.ts#L12
And here: https://github.com/ysabri/GitDrive/blob/master/src/git-drive/git/core-git.ts#L51

If you want to generalize the approach I think Dugite should also include resolving where the error came from. The problem is Git returns errors either in stdout or stderr, which explains the additional infrastructure you guys did in GitHub Desktop.

What I don't like in Dugite is that the responsibility of figuring out where the error is falls on the caller. This can be handled/abstracted by Dugite based on regex matching (like you do already) or based on matching error codes to operations.

I hope this helps and excuse any misunderstanding, it has been a while since I have worked on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants