cli - handle unhandled errors (fix #128854)

This commit is contained in:
Benjamin Pasero 2021-08-04 13:43:07 +02:00
parent 78a478fba1
commit e007ca899c
No known key found for this signature in database
GPG key ID: E6380CC4C8219E65
3 changed files with 18 additions and 11 deletions

View file

@ -175,8 +175,8 @@ export class CodeApplication extends Disposable {
private registerListeners(): void {
// We handle uncaught exceptions here to prevent electron from opening a dialog to the user
setUnexpectedErrorHandler(err => this.onUnexpectedError(err));
process.on('uncaughtException', err => this.onUnexpectedError(err));
setUnexpectedErrorHandler(error => this.onUnexpectedError(error));
process.on('uncaughtException', error => onUnexpectedError(error));
process.on('unhandledRejection', (reason: unknown) => onUnexpectedError(reason));
// Dispose on shutdown
@ -328,22 +328,22 @@ export class CodeApplication extends Disposable {
return URI.file(path);
}
private onUnexpectedError(err: Error): void {
if (err) {
private onUnexpectedError(error: Error): void {
if (error) {
// take only the message and stack property
const friendlyError = {
message: `[uncaught exception in main]: ${err.message}`,
stack: err.stack
message: `[uncaught exception in main]: ${error.message}`,
stack: error.stack
};
// handle on client side
this.windowsMainService?.sendToFocused('vscode:reportError', JSON.stringify(friendlyError));
}
this.logService.error(`[uncaught exception in main]: ${err}`);
if (err.stack) {
this.logService.error(err.stack);
this.logService.error(`[uncaught exception in main]: ${error}`);
if (error.stack) {
this.logService.error(error.stack);
}
}

View file

@ -42,7 +42,7 @@ import { ExtensionManagementCLIService } from 'vs/platform/extensionManagement/c
import { URI } from 'vs/base/common/uri';
import { LocalizationsService } from 'vs/platform/localizations/node/localizations';
import { ILocalizationsService } from 'vs/platform/localizations/common/localizations';
import { setUnexpectedErrorHandler } from 'vs/base/common/errors';
import { onUnexpectedError, setUnexpectedErrorHandler } from 'vs/base/common/errors';
import { toErrorMessage } from 'vs/base/common/errorMessage';
import { VSBuffer } from 'vs/base/common/buffer';
import { cwd } from 'vs/base/common/process';
@ -189,6 +189,10 @@ class CliMain extends Disposable {
logService.error(`[uncaught exception in CLI]: ${message}`);
});
// Handle unhandled errors that can occur
process.on('uncaughtException', err => onUnexpectedError(err));
process.on('unhandledRejection', (reason: unknown) => onUnexpectedError(reason));
}
private async doRun(environmentService: INativeEnvironmentService, extensionManagementCLIService: IExtensionManagementCLIService, fileService: IFileService): Promise<void> {

View file

@ -1151,7 +1151,10 @@ export class FileService extends Disposable implements IFileService {
override dispose(): void {
super.dispose();
this.activeWatchers.forEach(watcher => dispose(watcher.disposable));
for (const [, watcher] of this.activeWatchers) {
dispose(watcher.disposable);
}
this.activeWatchers.clear();
}