Skip to content

Commit

Permalink
fix #144555
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Apr 25, 2022
1 parent fef2fb9 commit 310108c
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/vs/platform/download/common/downloadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Schemas } from 'vs/base/common/network';
import { URI } from 'vs/base/common/uri';
import { IDownloadService } from 'vs/platform/download/common/download';
import { IFileService } from 'vs/platform/files/common/files';
import { asText, IRequestService } from 'vs/platform/request/common/request';
import { asTextOrError, IRequestService } from 'vs/platform/request/common/request';

export class DownloadService implements IDownloadService {

Expand All @@ -30,7 +30,7 @@ export class DownloadService implements IDownloadService {
if (context.res.statusCode === 200) {
await this.fileService.writeFile(target, context.stream);
} else {
const message = await asText(context);
const message = await asTextOrError(context);
throw new Error(`Expected 200, got back ${context.res.statusCode} instead.\n\n${message}`);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { isEngineValid } from 'vs/platform/extensions/common/extensionValidator'
import { IFileService } from 'vs/platform/files/common/files';
import { ILogService } from 'vs/platform/log/common/log';
import { IProductService } from 'vs/platform/product/common/productService';
import { asJson, asText, IRequestService, isSuccess } from 'vs/platform/request/common/request';
import { asJson, asTextOrError, IRequestService, isSuccess } from 'vs/platform/request/common/request';
import { resolveMarketplaceHeaders } from 'vs/platform/externalServices/common/marketplace';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
Expand Down Expand Up @@ -995,7 +995,7 @@ abstract class AbstractExtensionGalleryService implements IExtensionGalleryServi
async getReadme(extension: IGalleryExtension, token: CancellationToken): Promise<string> {
if (extension.assets.readme) {
const context = await this.getAsset(extension.assets.readme, {}, token);
const content = await asText(context);
const content = await asTextOrError(context);
return content || '';
}
return '';
Expand All @@ -1004,7 +1004,7 @@ abstract class AbstractExtensionGalleryService implements IExtensionGalleryServi
async getManifest(extension: IGalleryExtension, token: CancellationToken): Promise<IExtensionManifest | null> {
if (extension.assets.manifest) {
const context = await this.getAsset(extension.assets.manifest, {}, token);
const text = await asText(context);
const text = await asTextOrError(context);
return text ? JSON.parse(text) : null;
}
return null;
Expand All @@ -1024,7 +1024,7 @@ abstract class AbstractExtensionGalleryService implements IExtensionGalleryServi
const asset = extension.assets.coreTranslations.filter(t => t[0] === languageId.toUpperCase())[0];
if (asset) {
const context = await this.getAsset(asset[1]);
const text = await asText(context);
const text = await asTextOrError(context);
return text ? JSON.parse(text) : null;
}
return null;
Expand All @@ -1033,7 +1033,7 @@ abstract class AbstractExtensionGalleryService implements IExtensionGalleryServi
async getChangelog(extension: IGalleryExtension, token: CancellationToken): Promise<string> {
if (extension.assets.changelog) {
const context = await this.getAsset(extension.assets.changelog, {}, token);
const content = await asText(context);
const content = await asTextOrError(context);
return content || '';
}
return '';
Expand Down Expand Up @@ -1096,7 +1096,7 @@ abstract class AbstractExtensionGalleryService implements IExtensionGalleryServi
if (context.res.statusCode === 200) {
return context;
}
const message = await asText(context);
const message = await asTextOrError(context);
throw new Error(`Expected 200, got back ${context.res.statusCode} instead.\n\n${message}`);
} catch (err) {
if (isCancellationError(err)) {
Expand Down
10 changes: 7 additions & 3 deletions src/vs/platform/request/common/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@ function hasNoContent(context: IRequestContext): boolean {
}

export async function asText(context: IRequestContext): Promise<string | null> {
if (!isSuccess(context)) {
throw new Error('Server returned ' + context.res.statusCode);
}
if (hasNoContent(context)) {
return null;
}
const buffer = await streamToBuffer(context.stream);
return buffer.toString();
}

export async function asTextOrError(context: IRequestContext): Promise<string | null> {
if (!isSuccess(context)) {
throw new Error('Server returned ' + context.res.statusCode);
}
return asText(context);
}

export async function asJson<T = {}>(context: IRequestContext): Promise<T | null> {
if (!isSuccess(context)) {
throw new Error('Server returned ' + context.res.statusCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IFileService } from 'vs/platform/files/common/files';
import { IProductService } from 'vs/platform/product/common/productService';
import { asJson, asText, IRequestService, isSuccess as isSuccessContext } from 'vs/platform/request/common/request';
import { asJson, asTextOrError, IRequestService, isSuccess as isSuccessContext } from 'vs/platform/request/common/request';
import { getServiceMachineId } from 'vs/platform/externalServices/common/serviceMachineId';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { CONFIGURATION_SYNC_STORE_KEY, HEADER_EXECUTION_ID, HEADER_OPERATION_ID, IAuthenticationProvider, IResourceRefHandle, IUserData, IUserDataManifest, IUserDataSyncLogService, IUserDataSyncStore, IUserDataSyncStoreClient, IUserDataSyncStoreManagementService, IUserDataSyncStoreService, ServerResource, SYNC_SERVICE_URL_TYPE, UserDataSyncErrorCode, UserDataSyncStoreError, UserDataSyncStoreType } from 'vs/platform/userDataSync/common/userDataSync';
Expand Down Expand Up @@ -254,7 +254,7 @@ export class UserDataSyncStoreClient extends Disposable implements IUserDataSync
headers['Cache-Control'] = 'no-cache';

const context = await this.request(url, { type: 'GET', headers }, [], CancellationToken.None);
const content = await asText(context);
const content = await asTextOrError(context);
return content;
}

Expand Down Expand Up @@ -295,7 +295,7 @@ export class UserDataSyncStoreClient extends Disposable implements IUserDataSync
throw new UserDataSyncStoreError('Server did not return the ref', url, UserDataSyncErrorCode.NoRef, context.res.statusCode, context.res.headers[HEADER_OPERATION_ID]);
}

const content = await asText(context);
const content = await asTextOrError(context);
if (!content && context.res.statusCode === 304) {
throw new UserDataSyncStoreError('Empty response', url, UserDataSyncErrorCode.EmptyResponse, context.res.statusCode, context.res.headers[HEADER_OPERATION_ID]);
}
Expand Down Expand Up @@ -352,7 +352,7 @@ export class UserDataSyncStoreClient extends Disposable implements IUserDataSync
throw new UserDataSyncStoreError('Server did not return the ref', url, UserDataSyncErrorCode.NoRef, context.res.statusCode, context.res.headers[HEADER_OPERATION_ID]);
}

const content = await asText(context);
const content = await asTextOrError(context);
if (!content && context.res.statusCode === 304) {
throw new UserDataSyncStoreError('Empty response', url, UserDataSyncErrorCode.EmptyResponse, context.res.statusCode, context.res.headers[HEADER_OPERATION_ID]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/vs/server/node/webClientServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { FileAccess, connectionTokenCookieName, connectionTokenQueryName, Schema
import { generateUuid } from 'vs/base/common/uuid';
import { IProductService } from 'vs/platform/product/common/productService';
import { ServerConnectionToken, ServerConnectionTokenType } from 'vs/server/node/serverConnectionToken';
import { asText, IRequestService } from 'vs/platform/request/common/request';
import { asTextOrError, IRequestService } from 'vs/platform/request/common/request';
import { IHeaders } from 'vs/base/parts/request/common/request';
import { CancellationToken } from 'vs/base/common/cancellation';
import { URI } from 'vs/base/common/uri';
Expand Down Expand Up @@ -197,7 +197,7 @@ export class WebClientServer {
if (status !== 200) {
let text: string | null = null;
try {
text = await asText(context);
text = await asTextOrError(context);
} catch (error) {/* Ignore */ }
return serveError(req, res, status, text || `Request failed with status ${status}`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { Registry } from 'vs/platform/registry/common/platform';
import { IColorRegistry, Extensions, ColorContribution } from 'vs/platform/theme/common/colorRegistry';
import { asText } from 'vs/platform/request/common/request';
import { asTextOrError } from 'vs/platform/request/common/request';
import * as pfs from 'vs/base/node/pfs';
import * as path from 'vs/base/common/path';
import * as assert from 'assert';
Expand Down Expand Up @@ -39,7 +39,7 @@ suite('Color Registry', function () {
const environmentService = new class extends mock<INativeEnvironmentService>() { override args = { _: [] }; };

const reqContext = await new RequestService(new TestConfigurationService(), environmentService, new NullLogService()).request({ url: 'https://raw.githubusercontent.com/microsoft/vscode-docs/vnext/api/references/theme-color.md' }, CancellationToken.None);
const content = (await asText(reqContext))!;
const content = (await asTextOrError(reqContext))!;

const expression = /-\s*\`([\w\.]+)\`: (.*)/g;

Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/contrib/update/browser/releaseNotesEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { IProductService } from 'vs/platform/product/common/productService';
import { asText, IRequestService } from 'vs/platform/request/common/request';
import { asTextOrError, IRequestService } from 'vs/platform/request/common/request';
import { DEFAULT_MARKDOWN_STYLES, renderMarkdownDocument } from 'vs/workbench/contrib/markdown/browser/markdownDocumentRenderer';
import { WebviewInput } from 'vs/workbench/contrib/webviewPanel/browser/webviewEditorInput';
import { IWebviewWorkbenchService } from 'vs/workbench/contrib/webviewPanel/browser/webviewWorkbenchService';
Expand Down Expand Up @@ -163,7 +163,7 @@ export class ReleaseNotesManager {
const fetchReleaseNotes = async () => {
let text;
try {
text = await asText(await this._requestService.request({ url }, CancellationToken.None));
text = await asTextOrError(await this._requestService.request({ url }, CancellationToken.None));
} catch {
throw new Error('Failed to fetch release notes');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { IFileService } from 'vs/platform/files/common/files';
import { AbstractExtensionResourceLoaderService, IExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/common/extensionResourceLoader';
import { IProductService } from 'vs/platform/product/common/productService';
import { asText, IRequestService } from 'vs/platform/request/common/request';
import { asTextOrError, IRequestService } from 'vs/platform/request/common/request';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
Expand All @@ -31,7 +31,7 @@ export class ExtensionResourceLoaderService extends AbstractExtensionResourceLoa
if (this.isExtensionGalleryResource(uri)) {
const headers = await this.getExtensionGalleryRequestHeaders();
const requestContext = await this._requestService.request({ url: uri.toString(), headers }, CancellationToken.None);
return (await asText(requestContext)) || '';
return (await asTextOrError(requestContext)) || '';
}
const result = await this._fileService.readFile(uri);
return result.value.toString();
Expand Down

0 comments on commit 310108c

Please sign in to comment.