Skip to content

Commit e170e9e

Browse files
committed
Adds better tracing of failed git commands
1 parent 2d6bae1 commit e170e9e

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
8+
### Added
9+
- Adds better logging for failed git commands
10+
811
### Changed
912
- Marks temporary files (used when showing comparisions with previous revisions) as read-only to help avoid accidental edits/saving
1013

src/git/git.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,22 @@ async function gitCommandCore(options: CommandOptions & { readonly correlationKe
106106
}
107107

108108
let data: string;
109+
let exception: Error | undefined;
109110
try {
110111
data = await promise;
111112
}
113+
catch (ex) {
114+
exception = ex;
115+
throw ex;
116+
}
112117
finally {
113118
pendingCommands.delete(command);
114119

115120
const duration = process.hrtime(start);
116-
const completedIn = `in ${(duration[0] * 1000) + Math.floor(duration[1] / 1000000)} ms`;
121+
const completedIn = `${exception === undefined ? 'Completed' : 'FAILED'} in ${(duration[0] * 1000) + Math.floor(duration[1] / 1000000)} ms`;
117122

118-
Logger.log(`Completed${command} ${completedIn}`);
119-
Logger.logGitCommand(`${gitCommand} ${completedIn}`, runOpts.cwd!);
123+
Logger.log(`${exception === undefined ? 'Completed' : 'FAILED'}${command} ${completedIn}`);
124+
Logger.logGitCommand(`${gitCommand} ${completedIn}`, runOpts.cwd!, exception);
120125
}
121126

122127
if (encoding === 'utf8' || encoding === 'binary') return data;
@@ -129,13 +134,13 @@ function gitCommandDefaultErrorHandler(ex: Error, options: CommandOptions, ...ar
129134
if (msg) {
130135
for (const warning of Objects.values(GitWarnings)) {
131136
if (warning.test(msg)) {
132-
Logger.warn('git', ...args, ` cwd='${options.cwd}'`, `\n ${msg.replace(/\r?\n|\r/g, ' ')}`);
137+
Logger.warn('git', ...args, ` cwd='${options.cwd}'\n\n `, msg.replace(/\r?\n|\r/g, ' '));
133138
return '';
134139
}
135140
}
136141
}
137142

138-
Logger.error(ex, 'git', ...args, ` cwd='${options.cwd}'`, msg && `\n ${msg.replace(/\r?\n|\r/g, ' ')}`);
143+
Logger.error(ex, 'git', ...args, ` cwd='${options.cwd}'\n\n `);
139144
throw ex;
140145
}
141146

src/logger.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ export class Logger {
5353

5454
static error(ex: Error, classOrMethod?: string, ...params: any[]): void {
5555
if (this.debug) {
56-
console.error(this.timestamp, ConsolePrefix, classOrMethod, ex, ...params);
56+
console.error(this.timestamp, ConsolePrefix, classOrMethod, ...params, ex);
5757
}
5858

5959
if (this.output !== undefined && this.level !== OutputLevel.Silent) {
60-
this.output.appendLine((this.debug ? [this.timestamp, classOrMethod, ex, ...params] : [classOrMethod, ex, ...params]).join(' '));
60+
this.output.appendLine((this.debug ? [this.timestamp, classOrMethod, ...params, ex] : [classOrMethod, ...params, ex]).join(' '));
6161
}
6262

6363
// Telemetry.trackException(ex);
@@ -80,12 +80,12 @@ export class Logger {
8080

8181
static gitOutput: OutputChannel | undefined;
8282

83-
static logGitCommand(command: string, cwd: string): void {
83+
static logGitCommand(command: string, cwd: string, ex?: Error): void {
8484
if (this.level !== OutputLevel.Debug) return;
8585

8686
if (this.gitOutput === undefined) {
8787
this.gitOutput = window.createOutputChannel(`${ExtensionOutputChannelName} (Git)`);
8888
}
89-
this.gitOutput.appendLine(`${this.timestamp} ${command} (${cwd})`);
89+
this.gitOutput.appendLine(`${this.timestamp} ${command} (${cwd})${ex === undefined ? '' : `\n\n${ex.toString()}`}`);
9090
}
9191
}

0 commit comments

Comments
 (0)