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

🚸 Warn user if they are not in a git repository #1276

Merged
merged 2 commits into from Mar 21, 2024

Conversation

mikelinhas
Copy link
Contributor

@mikelinhas mikelinhas commented Mar 12, 2024

The change introduces a check to bail out if the user runs the tool outside a git repository. It will also warn the user. This handles this common pitfall gracefully instead of logging the stdout errors that happen in these cases.

Description

Issue: #1275

Tests

  • All tests passed.

Did not create a new test for this.

Copy link
Owner

@carloscuesta carloscuesta left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! 🙌🏼


const withClient = async (answers: Answers): Promise<void> => {
try {
if(!isGitRepository()) {
Copy link
Owner

Choose a reason for hiding this comment

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

I would probably avoid handling this through a helper and instead improve the way we show errors to users.

This way we can catch all exceptions handling them gracefully

Comment on lines 12 to 19
if(!isGitRepository()) {
return console.log(
chalk.red(
"\nError: Seems that you're trying to commit outside a git repository \n" +
'Please navigate to a git repository to use `gitmoji` \n'
)
)
}
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
if(!isGitRepository()) {
return console.log(
chalk.red(
"\nError: Seems that you're trying to commit outside a git repository \n" +
'Please navigate to a git repository to use `gitmoji` \n'
)
)
}

Copy link
Owner

Choose a reason for hiding this comment

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

This is what's causing the error to display in a weird way, we can just remove this:

} catch (error) {
console.error(error)
}
}

Copy link
Owner

Choose a reason for hiding this comment

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

Then handle the exception here:

} catch (error) {
console.error(
chalk.red(
'\n',
'Oops! An error occurred. There is likely additional logging output above.\n',
'You can run the same commit with this command:\n'
),
'\t',
error.escapedCommand
)
}

Copy link
Owner

Choose a reason for hiding this comment

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

We can use error.stderr, error.command & error.shortMessage to see if we can display a meaningful error, otherwise fallback to what we have

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only issue I see is that it will not be a controlled error.
We won't know inside the catch block what has actually happened.

Another thing to note - if we don't bail out with the helper, we ask the user for input knowing they are kind of doomed to fail. 🙈

Copy link
Collaborator

Choose a reason for hiding this comment

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

That original error handling was nice, since it told you the git command it was going to execute before it failed.

That was only because it didn't exit early in the flow and it eventually failed on the last step, being git commit. So logically we need to adjust the flow or error message slightly to make sense now. Ideally we just try catch execa executions and handle each use case accordingly (eg. not all of them need to mention the command retry).

Something like this could work (rough idea):

import chalk from 'chalk'
import { ExecaError } from 'execa'

const printExecaError = (error: ExecaError, includeCommandRetry = false) => {
  let message = `${error.stderr}\n${error.shortMessage}\n\nOops! An error occurred. There is likely additional logging output above.\n`

  if (includeCommandRetry) {
    message += `You can run the same commit with this command:\n${error.escapedCommand}`
  }

  console.error(chalk.red('\n', message))
}

export default printExecaError

Another alternative would be if we custom wrapped execa which then throws custom controlled ExecutionError's that we could then use to check instanceof and treat accordingly in a general try catch. Would allow us to properly differentiate between general runtime errors and execa errors (we don't currently do that and assume that all errors are execa related).

But I'll leave the final decision up to @carloscuesta to decide the direction to go in.

Copy link
Owner

Choose a reason for hiding this comment

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

Both are fine!

I don't think that loosing the escapedCommand thingy is too much of a loss, if we improve the overall error handling

So feel free to go with any of the options!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great! Thanks.
I removed the initial approach and just made the error handling more generic.
Hope it helps.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for making the required changes! Ideally the files you touched are also converted to TS 🙏 But don't worry if you're not up to it. I can always open another PR with those changes after this one is merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm a bit weary of migrating to Typescript file by file.
I might be doing it wrong, but as soon as I change one file, I have to basically change all the depending files and tests in a cascading effect.
I'll keep an eye on the project and lend a hand if I figure out how you're doing it.

src/utils/isGitRepository.js Outdated Show resolved Hide resolved
Miguel Zarco added 2 commits March 20, 2024 14:25
The change introduces a check to bail out if the user runs the tool outside a git repository. It will also warn the user. This handles this common pitfall gracefully instead of logging the stdout errors that happen in these cases.
Copy link

codecov bot commented Mar 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.72%. Comparing base (6bf779d) to head (c8f8af3).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1276      +/-   ##
==========================================
+ Coverage   91.42%   91.72%   +0.29%     
==========================================
  Files          28       28              
  Lines         280      278       -2     
  Branches       66       66              
==========================================
- Hits          256      255       -1     
+ Misses         23       22       -1     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Owner

@carloscuesta carloscuesta left a comment

Choose a reason for hiding this comment

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

Thanks!!

@carloscuesta carloscuesta merged commit ac26d76 into carloscuesta:master Mar 21, 2024
3 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 23, 2024
@mikelinhas mikelinhas deleted the add-git-repo-check branch March 25, 2024 07:38
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants