Skip to content

Commit

Permalink
Adds better git error handling/logging
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Apr 18, 2019
1 parent 3c31d1a commit dd130f8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
51 changes: 30 additions & 21 deletions src/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export async function git<TOut extends string | Buffer>(options: GitCommandOptio
throw ex;

default: {
const result = defaultExceptionHandler(ex, options, ...args);
const result = defaultExceptionHandler(ex, options.cwd, start);
exception = undefined;
return result as TOut;
}
Expand All @@ -186,40 +186,49 @@ export async function git<TOut extends string | Buffer>(options: GitCommandOptio
pendingCommands.delete(command);

const duration = `${Strings.getDurationMilliseconds(start)} ms ${waiting ? '(await) ' : emptyStr}`;
Logger.log(
`${gitCommand} ${GlyphChars.Dot} ${
exception !== undefined
? `FAILED(${(exception.message || emptyStr).trim().split('\n', 1)[0]}) `
: emptyStr
}${duration}`
);
if (exception !== undefined) {
Logger.warn(
`[${runOpts.cwd}] Git ${(exception.message || exception.toString() || emptyStr)
.trim()
.replace(/fatal: /g, '')
.replace(/\r?\n|\r/g, ` ${GlyphChars.Dot} `)} ${GlyphChars.Dot} ${duration}`
);
}
else {
Logger.log(`${gitCommand} ${GlyphChars.Dot} ${duration}`);
}
Logger.logGitCommand(
`${gitCommand} ${GlyphChars.Dot} ${exception !== undefined ? 'FAILED ' : emptyStr}${duration}`,
exception
);
}
}

function defaultExceptionHandler(ex: Error, options: GitCommandOptions, ...args: any[]): string {
const msg = ex && ex.toString();
if (msg) {
function defaultExceptionHandler(ex: Error, cwd: string | undefined, start?: [number, number]): string {
const msg = ex.message || ex.toString();
if (msg != null && msg.length !== 0) {
for (const warning of Objects.values(GitWarnings)) {
if (warning.test(msg)) {
Logger.warn('git', ...args, ` cwd='${options.cwd}'\n\n `, msg.replace(/\r?\n|\r/g, ' '));
const duration = start !== undefined ? `${Strings.getDurationMilliseconds(start)} ms` : emptyStr;
Logger.warn(
`[${cwd}] Git ${msg
.trim()
.replace(/fatal: /g, '')
.replace(/\r?\n|\r/g, ` ${GlyphChars.Dot} `)} ${GlyphChars.Dot} ${duration}`
);
return emptyStr;
}
}
}

const match = GitErrors.badRevision.exec(msg);
if (match != null && match) {
const [, ref] = match;
const match = GitErrors.badRevision.exec(msg);
if (match != null && match) {
const [, ref] = match;

// Since looking up a ref with ^3 (e.g. looking for untracked files in a stash) can error on some versions of git just ignore it
if (ref != null && ref.endsWith('^3')) return emptyStr;
// Since looking up a ref with ^3 (e.g. looking for untracked files in a stash) can error on some versions of git just ignore it
if (ref != null && ref.endsWith('^3')) return emptyStr;
}
}

Logger.error(ex, 'git', ...args, ` cwd='${options.cwd}'\n\n `);
throw ex;
}

Expand Down Expand Up @@ -798,7 +807,7 @@ export class Git {
return data.length === 0 ? undefined : [data.trim(), undefined];
}

defaultExceptionHandler(ex, opts, ...params);
defaultExceptionHandler(ex, opts.cwd);
return undefined;
}
}
Expand Down Expand Up @@ -853,7 +862,7 @@ export class Git {
return undefined;
}

return defaultExceptionHandler(ex, opts, args) as TOut;
return defaultExceptionHandler(ex, opts.cwd) as TOut;
}
}

Expand Down
31 changes: 11 additions & 20 deletions src/git/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,6 @@ export function findExecutable(exe: string, args: string[]): { cmd: string; args
return { cmd: exe, args: args };
}

export class RunError extends Error {
constructor(public readonly exitCode: number, ...args: any[]) {
super(...args);

Error.captureStackTrace(this, RunError);
}
}

export interface RunOptions {
cwd?: string;
readonly env?: Record<string, any>;
Expand All @@ -132,6 +124,8 @@ export interface RunOptions {
readonly stdinEncoding?: string;
}

const bufferExceededRegex = /stdout maxBuffer( length)? exceeded/;

export function run<TOut extends string | Buffer>(
command: string,
args: any[],
Expand All @@ -145,18 +139,15 @@ export function run<TOut extends string | Buffer>(
command,
args,
opts,
(err: (Error & { code?: string | number }) | null, stdout, stderr) => {
if (err != null) {
reject(
new RunError(
err.code ? Number(err.code) : 0,
err.message === 'stdout maxBuffer exceeded'
? `Command output exceeded the allocated stdout buffer. Set 'options.maxBuffer' to a larger value than ${
opts.maxBuffer
} bytes`
: stderr || stdout
)
);
(error: (Error & { code?: string | number }) | null, stdout, stderr) => {
if (error != null) {
if (bufferExceededRegex.test(error.message)) {
error.message = `Command output exceeded the allocated stdout buffer. Set 'options.maxBuffer' to a larger value than ${
opts.maxBuffer
} bytes`;
}

reject(error);

return;
}
Expand Down

0 comments on commit dd130f8

Please sign in to comment.