Skip to content

Catch errors from git being absent and continue on as best possible - #173

Merged
Noviny merged 7 commits into
masterfrom
catch-git-errors
Oct 4, 2019
Merged

Noviny merged 7 commits into
masterfrom
catch-git-errors

Conversation

@ajaymathur

Copy link
Copy Markdown
Contributor

No description provided.

@github-actions

github-actions Bot commented Oct 1, 2019 •

Copy link
Copy Markdown
Contributor

🦋 Changeset is good to go

Latest commit: 74f553d

We got this.

Not sure what this means? Click here to learn what changesets are.

@ajaymathur ajaymathur changed the title For #130 Catch errors from git being absent and continue on as best possible Oct 1, 2019
Comment thread packages/git/src/index.ts Outdated
Comment on lines +148 to +154
try {
const masterRef = await getMasterRef(cwd);
return getChangedPackagesSinceCommit(masterRef, cwd);
} catch {
// Do nothing when operation fails
}
return [];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not able to get it tests in unit test properly. If you have encountered this type of condition please let me know the best way to test it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it should theoretically never happen, let's not wrap it in a try catch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't wrap this in try catch, then error will bubble up and it will return an empty array they way we want it to.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, I thought this function was calling out to the other one that includes a try catch, makes sense.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrt testing: using cwd = await copyFixtureIntoTempDir(__dirname, "with-git") in a test but without running git init should make it fail.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I will add a test.

Comment thread packages/git/src/index.ts Outdated
Comment on lines +148 to +154
try {
const masterRef = await getMasterRef(cwd);
return getChangedPackagesSinceCommit(masterRef, cwd);
} catch {
// Do nothing when operation fails
}
return [];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it should theoretically never happen, let's not wrap it in a try catch.

Comment thread packages/git/src/index.ts Outdated
.filter(file => tester.test(file));
if (!fullPath) return files;
return files.map(file => path.resolve(cwd, file));
} catch {}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make this specific to the git errors so we don't silently catch errors from actual potential bugs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good pattern is:

try {
  // some code at some time
} catch (e) {
  if (e.message.includes('this-specific-thing') {
    // handle the error
  } else {
    throw e
  }
}

message is one option, but there may be better things to interrogate.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I like doing is this

class GitError extends Error {}


try {
  // some code at some time
  throw new GitError('the message')
} catch (e) {
  if (e instanceof GitError) {
    // handle the error
  } else {
    throw e
  }
}

@ajaymathur

Copy link
Copy Markdown
Contributor Author

@Noviny @mitchellhamilton Thanks for the review, PR is ready for review again 🙂

@ajaymathur

Copy link
Copy Markdown
Contributor Author

@mitchellhamilton Thanks for adding changeset and updating readme.

I actually added the package as @changesets/errors since in future there will more that one error type exported from the package. But yeah now I see having it as error is better. I can update it if you are not already doing it. 😉

@emmatown

emmatown commented Oct 4, 2019

Copy link
Copy Markdown
Member

I actually added the package as @changesets/errors since in future there will more that one error type exported from the package. But yeah now I see having it as error is better. I can update it if you are not already doing it. 😉

Totally agree with having @changesets/errors(including the name), that update to the readme was making it consistent with the name in the package.json

@ajaymathur

Copy link
Copy Markdown
Contributor Author

I read your commit in a wrong way. Thanks for correcting it, 🙂

@Noviny Noviny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed one last thing - I think this is good to merge

@Noviny
Noviny merged commit 94de7c1 into master Oct 4, 2019
@Noviny
Noviny deleted the catch-git-errors branch October 4, 2019 06:26
@github-actions github-actions Bot mentioned this pull request Oct 4, 2019
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

Successfully merging this pull request may close these issues.

3 participants