Browse Source

Adds better tracing of failed git commands

main
Eric Amodio 6 years ago
parent
commit
e170e9ebd3
3 changed files with 17 additions and 9 deletions
  1. +3
    -0
      CHANGELOG.md
  2. +10
    -5
      src/git/git.ts
  3. +4
    -4
      src/logger.ts

+ 3
- 0
CHANGELOG.md View File

@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased] ## [Unreleased]
### Added
- Adds better logging for failed git commands
### Changed ### Changed
- Marks temporary files (used when showing comparisions with previous revisions) as read-only to help avoid accidental edits/saving - Marks temporary files (used when showing comparisions with previous revisions) as read-only to help avoid accidental edits/saving

+ 10
- 5
src/git/git.ts View File

@ -106,17 +106,22 @@ async function gitCommandCore(options: CommandOptions & { readonly correlationKe
} }
let data: string; let data: string;
let exception: Error | undefined;
try { try {
data = await promise; data = await promise;
} }
catch (ex) {
exception = ex;
throw ex;
}
finally { finally {
pendingCommands.delete(command); pendingCommands.delete(command);
const duration = process.hrtime(start); const duration = process.hrtime(start);
const completedIn = `in ${(duration[0] * 1000) + Math.floor(duration[1] / 1000000)} ms`;
const completedIn = `${exception === undefined ? 'Completed' : 'FAILED'} in ${(duration[0] * 1000) + Math.floor(duration[1] / 1000000)} ms`;
Logger.log(`Completed${command} ${completedIn}`);
Logger.logGitCommand(`${gitCommand} ${completedIn}`, runOpts.cwd!);
Logger.log(`${exception === undefined ? 'Completed' : 'FAILED'}${command} ${completedIn}`);
Logger.logGitCommand(`${gitCommand} ${completedIn}`, runOpts.cwd!, exception);
} }
if (encoding === 'utf8' || encoding === 'binary') return data; if (encoding === 'utf8' || encoding === 'binary') return data;
@ -129,13 +134,13 @@ function gitCommandDefaultErrorHandler(ex: Error, options: CommandOptions, ...ar
if (msg) { if (msg) {
for (const warning of Objects.values(GitWarnings)) { for (const warning of Objects.values(GitWarnings)) {
if (warning.test(msg)) { if (warning.test(msg)) {
Logger.warn('git', ...args, ` cwd='${options.cwd}'`, `\n ${msg.replace(/\r?\n|\r/g, ' ')}`);
Logger.warn('git', ...args, ` cwd='${options.cwd}'\n\n `, msg.replace(/\r?\n|\r/g, ' '));
return ''; return '';
} }
} }
} }
Logger.error(ex, 'git', ...args, ` cwd='${options.cwd}'`, msg && `\n ${msg.replace(/\r?\n|\r/g, ' ')}`);
Logger.error(ex, 'git', ...args, ` cwd='${options.cwd}'\n\n `);
throw ex; throw ex;
} }

+ 4
- 4
src/logger.ts View File

@ -53,11 +53,11 @@ export class Logger {
static error(ex: Error, classOrMethod?: string, ...params: any[]): void { static error(ex: Error, classOrMethod?: string, ...params: any[]): void {
if (this.debug) { if (this.debug) {
console.error(this.timestamp, ConsolePrefix, classOrMethod, ex, ...params);
console.error(this.timestamp, ConsolePrefix, classOrMethod, ...params, ex);
} }
if (this.output !== undefined && this.level !== OutputLevel.Silent) { if (this.output !== undefined && this.level !== OutputLevel.Silent) {
this.output.appendLine((this.debug ? [this.timestamp, classOrMethod, ex, ...params] : [classOrMethod, ex, ...params]).join(' '));
this.output.appendLine((this.debug ? [this.timestamp, classOrMethod, ...params, ex] : [classOrMethod, ...params, ex]).join(' '));
} }
// Telemetry.trackException(ex); // Telemetry.trackException(ex);
@ -80,12 +80,12 @@ export class Logger {
static gitOutput: OutputChannel | undefined; static gitOutput: OutputChannel | undefined;
static logGitCommand(command: string, cwd: string): void {
static logGitCommand(command: string, cwd: string, ex?: Error): void {
if (this.level !== OutputLevel.Debug) return; if (this.level !== OutputLevel.Debug) return;
if (this.gitOutput === undefined) { if (this.gitOutput === undefined) {
this.gitOutput = window.createOutputChannel(`${ExtensionOutputChannelName} (Git)`); this.gitOutput = window.createOutputChannel(`${ExtensionOutputChannelName} (Git)`);
} }
this.gitOutput.appendLine(`${this.timestamp} ${command} (${cwd})`);
this.gitOutput.appendLine(`${this.timestamp} ${command} (${cwd})${ex === undefined ? '' : `\n\n${ex.toString()}`}`);
} }
} }

Loading…
Cancel
Save