Skip to content

Commit

Permalink
🚒 Updated error handling for git commands
Browse files Browse the repository at this point in the history
  • Loading branch information
juriadams committed Mar 3, 2021
1 parent e346d39 commit 797f84d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 34 deletions.
33 changes: 22 additions & 11 deletions src/modules/git/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,34 @@ const getGitBranch = async (verbose?: boolean): Promise<string | null> => {
if (verbose) signale.scope("git branch").debug(`Executing \`${command}\``);

// Execute Command
const { stdout, stderr } = await execAsync(command);

// Catch Errors
if (stderr) {
try {
const { stdout, stderr } = await execAsync(command);

// Catch Errors
if (stderr) {
signale
.scope("git branch")
.warn(
`Error fetching git branch, command \`${command}\` failed`
);
if (verbose) signale.error(stderr);

return null;
}

if (verbose)
signale
.scope("git branch")
.debug(`Found git branch \`${stdout.replace("\n", "")}\``);
return stdout.replace("\n", "") || null;
} catch (error) {
signale
.scope("git branch")
.warn(`Error fetching git branch, command \`${command}\` failed`);
if (verbose) signale.error(stderr);
if (verbose) signale.error(error);

return null;
}

if (verbose)
signale
.scope("git branch")
.debug(`Found git branch \`${stdout.replace("\n", "")}\``);
return stdout.replace("\n", "") || null;
};

export default getGitBranch;
34 changes: 23 additions & 11 deletions src/modules/git/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,37 @@ const getGitCommitHash = async (verbose?: boolean): Promise<string | null> => {
if (verbose) signale.scope("git commit").debug(`Executing \`${command}\``);

// Execute Command
const { stdout, stderr } = await execAsync(command);

// Catch Errors
if (stderr) {
try {
// Execute Command
const { stdout, stderr } = await execAsync(command);

// Catch Errors
if (stderr) {
signale
.scope("git commit")
.warn(
`Error fetching latest commit, command \`${command}\` failed`
);
if (verbose) signale.error(stderr);

return null;
}

if (verbose)
signale
.scope("git commit")
.debug(`Found latest commit \`${stdout.replace("\n", "")}\``);
return stdout.replace("\n", "") || null;
} catch (error) {
signale
.scope("git commit")
.warn(
`Error fetching latest commit, command \`${command}\` failed`
);
if (verbose) signale.error(stderr);
if (verbose) signale.error(error);

return null;
}

if (verbose)
signale
.scope("git commit")
.debug(`Found latest comit \`${stdout.replace("\n", "")}\``);
return stdout.replace("\n", "") || null;
};

export default getGitCommitHash;
33 changes: 21 additions & 12 deletions src/modules/git/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,33 @@ const getGitUser = async (verbose?: boolean): Promise<string | null> => {
// Debug logging
if (verbose) signale.scope("git user").debug(`Executing \`${command}\``);

// Execute Command
const { stdout, stderr } = await execAsync(command);

// Catch Errors
if (stderr) {
try {
// Execute Command
const { stdout, stderr } = await execAsync(command);

// Catch Errors
if (stderr) {
signale
.scope("git user")
.warn(`Error fetching git user, command \`${command}\` failed`);
if (verbose) signale.error(stderr);

return null;
}

if (verbose)
signale
.scope("git user")
.debug(`Found git user \`${stdout.replace("\n", "")}\``);
return stdout.replace("\n", "") || null;
} catch (error) {
signale
.scope("git user")
.warn(`Error fetching git user, command \`${command}\` failed`);
if (verbose) signale.error(stderr);
if (verbose) signale.error(error);

return null;
}

if (verbose)
signale
.scope("git user")
.debug(`Found git user \`${stdout.replace("\n", "")}\``);
return stdout.replace("\n", "") || null;
};

export default getGitUser;

0 comments on commit 797f84d

Please sign in to comment.