Skip to content

Commit b87304b

Browse files
authored
Merge pull request microsoft#136947 from microsoft/sandy081/fix-136710
Handle all errors
2 parents 8722ab1 + 3c01307 commit b87304b

File tree

1 file changed

+50
-33
lines changed

1 file changed

+50
-33
lines changed

src/vs/platform/extensionManagement/common/abstractExtensionManagementService.ts

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -84,42 +84,11 @@ export abstract class AbstractExtensionManagementService extends Disposable impl
8484
}
8585

8686
async installFromGallery(extension: IGalleryExtension, options: InstallOptions = {}): Promise<ILocalExtension> {
87-
if (!this.galleryService.isEnabled()) {
88-
throw new ExtensionManagementError(nls.localize('MarketPlaceDisabled', "Marketplace is not enabled"), ExtensionManagementErrorCode.Internal);
89-
}
90-
91-
if (!await this.canInstall(extension)) {
92-
const targetPlatform = await this.getTargetPlatform();
93-
const error = new ExtensionManagementError(nls.localize('incompatible platform', "The '{0}' extension is not available in {1} for {2}.", extension.identifier.id, this.productService.nameLong, TargetPlatformToString(targetPlatform)), ExtensionManagementErrorCode.Incompatible);
94-
this.logService.error(`Cannot install extension.`, extension.identifier.id, error.message);
95-
reportTelemetry(this.telemetryService, 'extensionGallery:install', getGalleryExtensionTelemetryData(extension), undefined, error);
96-
throw error;
97-
}
98-
9987
try {
100-
extension = await this.checkAndGetCompatibleVersion(extension, !options.installGivenVersion);
88+
return await this.doInstallFromGallery(extension, options);
10189
} catch (error) {
102-
this.logService.error(getErrorMessage(error));
103-
reportTelemetry(this.telemetryService, 'extensionGallery:install', getGalleryExtensionTelemetryData(extension), undefined, error);
104-
throw error;
90+
throw toExtensionManagementError(error);
10591
}
106-
107-
const manifest = await this.galleryService.getManifest(extension, CancellationToken.None);
108-
if (manifest === null) {
109-
const error = new ExtensionManagementError(`Missing manifest for extension ${extension.identifier.id}`, ExtensionManagementErrorCode.Invalid);
110-
this.logService.error(`Failed to install extension:`, extension.identifier.id, error.message);
111-
reportTelemetry(this.telemetryService, 'extensionGallery:install', getGalleryExtensionTelemetryData(extension), undefined, error);
112-
throw error;
113-
}
114-
115-
if (manifest.version !== extension.version) {
116-
const error = new ExtensionManagementError(`Cannot install '${extension.identifier.id}' extension because of version mismatch in Marketplace`, ExtensionManagementErrorCode.Invalid);
117-
this.logService.error(error.message);
118-
reportTelemetry(this.telemetryService, 'extensionGallery:install', getGalleryExtensionTelemetryData(extension), undefined, error);
119-
throw error;
120-
}
121-
122-
return this.installExtension(manifest, extension, options);
12392
}
12493

12594
async uninstall(extension: ILocalExtension, options: UninstallOptions = {}): Promise<void> {
@@ -157,6 +126,45 @@ export abstract class AbstractExtensionManagementService extends Disposable impl
157126
this.participants.push(participant);
158127
}
159128

129+
private async doInstallFromGallery(extension: IGalleryExtension, options: InstallOptions = {}): Promise<ILocalExtension> {
130+
if (!this.galleryService.isEnabled()) {
131+
throw new ExtensionManagementError(nls.localize('MarketPlaceDisabled', "Marketplace is not enabled"), ExtensionManagementErrorCode.Internal);
132+
}
133+
134+
if (!await this.canInstall(extension)) {
135+
const targetPlatform = await this.getTargetPlatform();
136+
const error = new ExtensionManagementError(nls.localize('incompatible platform', "The '{0}' extension is not available in {1} for {2}.", extension.identifier.id, this.productService.nameLong, TargetPlatformToString(targetPlatform)), ExtensionManagementErrorCode.Incompatible);
137+
this.logService.error(`Cannot install extension.`, extension.identifier.id, error.message);
138+
reportTelemetry(this.telemetryService, 'extensionGallery:install', getGalleryExtensionTelemetryData(extension), undefined, error);
139+
throw error;
140+
}
141+
142+
try {
143+
extension = await this.checkAndGetCompatibleVersion(extension, !options.installGivenVersion);
144+
} catch (error) {
145+
this.logService.error(getErrorMessage(error));
146+
reportTelemetry(this.telemetryService, 'extensionGallery:install', getGalleryExtensionTelemetryData(extension), undefined, error);
147+
throw error;
148+
}
149+
150+
const manifest = await this.galleryService.getManifest(extension, CancellationToken.None);
151+
if (manifest === null) {
152+
const error = new ExtensionManagementError(`Missing manifest for extension ${extension.identifier.id}`, ExtensionManagementErrorCode.Invalid);
153+
this.logService.error(`Failed to install extension:`, extension.identifier.id, error.message);
154+
reportTelemetry(this.telemetryService, 'extensionGallery:install', getGalleryExtensionTelemetryData(extension), undefined, error);
155+
throw error;
156+
}
157+
158+
if (manifest.version !== extension.version) {
159+
const error = new ExtensionManagementError(`Cannot install '${extension.identifier.id}' extension because of version mismatch in Marketplace`, ExtensionManagementErrorCode.Invalid);
160+
this.logService.error(error.message);
161+
reportTelemetry(this.telemetryService, 'extensionGallery:install', getGalleryExtensionTelemetryData(extension), undefined, error);
162+
throw error;
163+
}
164+
165+
return this.installExtension(manifest, extension, options);
166+
}
167+
160168
protected async installExtension(manifest: IExtensionManifest, extension: URI | IGalleryExtension, options: InstallOptions & InstallVSIXOptions): Promise<ILocalExtension> {
161169
// only cache gallery extensions tasks
162170
if (!URI.isUri(extension)) {
@@ -619,6 +627,15 @@ export function joinErrors(errorOrErrors: (Error | string) | (Array<Error | stri
619627
}, new Error(''));
620628
}
621629

630+
function toExtensionManagementError(error: Error): ExtensionManagementError {
631+
if (error instanceof ExtensionManagementError) {
632+
return error;
633+
}
634+
const e = new ExtensionManagementError(error.message, ExtensionManagementErrorCode.Internal);
635+
e.stack = error.stack;
636+
return e;
637+
}
638+
622639
export function reportTelemetry(telemetryService: ITelemetryService, eventName: string, extensionData: any, duration?: number, error?: Error): void {
623640
const errorcode = error ? error instanceof ExtensionManagementError ? error.code : ExtensionManagementErrorCode.Internal : undefined;
624641
/* __GDPR__

0 commit comments

Comments
 (0)