Skip to content

Commit 4fc36fa

Browse files
authored
avoid reporting cancellation as errors (#13552)
1 parent 35e5376 commit 4fc36fa

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

Extension/src/LanguageServer/copilotProviders.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface CopilotApi {
3131
uri: vscode.Uri,
3232
context: { flags: Record<string, unknown> },
3333
cancellationToken: vscode.CancellationToken
34-
) => Promise<{ entries: vscode.Uri[]; traits?: CopilotTrait[] }>
34+
) => Promise<{ entries: vscode.Uri[]; traits?: CopilotTrait[] } | undefined>
3535
): Disposable;
3636
getContextProviderAPI(version: string): Promise<ContextProviderApiV1 | undefined>;
3737
}
@@ -91,15 +91,18 @@ export async function registerRelatedFilesProvider(): Promise<void> {
9191
return { entries: await includesPromise, traits: await traitsPromise };
9292
}
9393
catch (exception) {
94-
try {
95-
const err: Error = exception as Error;
96-
logger.getOutputChannelLogger().appendLine(localize("copilot.relatedfilesprovider.error", "Error while retrieving result. Reason: {0}", err.message));
94+
// Avoid logging the error message if it is a cancellation error.
95+
if (exception instanceof vscode.CancellationError) {
96+
telemetryProperties["error"] = "cancellation";
97+
telemetryProperties["cancellation"] = "true";
98+
throw exception; // Rethrow the cancellation error to be handled by the caller.
99+
} else if (exception instanceof Error) {
100+
telemetryProperties["error"] = "true";
101+
logger.getOutputChannelLogger().appendLine(localize("copilot.relatedfilesprovider.error", "Error while retrieving result. Reason: {0}", exception.message));
97102
}
98-
catch {
99-
// Intentionally swallow any exception.
100-
}
101-
telemetryProperties["error"] = "true";
102-
throw exception; // Throw the exception for auto-retry.
103+
104+
// In case of error retrieving the include files, we signal the caller of absence of the results by returning undefined.
105+
return undefined;
103106
} finally {
104107
telemetryMetrics['duration'] = performance.now() - start;
105108
telemetry.logCopilotEvent('RelatedFilesProvider', telemetryProperties, telemetryMetrics);

Extension/test/scenarios/SingleRootProject/tests/copilotProviders.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('copilotProviders Tests', () => {
2424
let getClientsStub: sinon.SinonStub;
2525
let activeClientStub: sinon.SinonStubbedInstance<DefaultClient>;
2626
let vscodeGetExtensionsStub: sinon.SinonStub;
27-
let callbackPromise: Promise<{ entries: vscode.Uri[]; traits?: CopilotTrait[] }> | undefined;
27+
let callbackPromise: Promise<{ entries: vscode.Uri[]; traits?: CopilotTrait[] } | undefined> | undefined;
2828
let vscodeExtension: vscode.Extension<unknown>;
2929
let telemetryStub: sinon.SinonStub;
3030

@@ -52,7 +52,7 @@ describe('copilotProviders Tests', () => {
5252
uri: vscode.Uri,
5353
context: { flags: Record<string, unknown> },
5454
cancellationToken: vscode.CancellationToken
55-
) => Promise<{ entries: vscode.Uri[]; traits?: CopilotTrait[] }>
55+
) => Promise<{ entries: vscode.Uri[]; traits?: CopilotTrait[] } | undefined>
5656
): vscode.Disposable & { [Symbol.dispose](): void } {
5757
return {
5858
dispose: () => { },
@@ -88,13 +88,13 @@ describe('copilotProviders Tests', () => {
8888
});
8989

9090
const arrange = ({ vscodeExtension, getIncludeFiles, projectContext, rootUri, flags }:
91-
{ vscodeExtension?: vscode.Extension<unknown>; getIncludeFiles?: GetIncludesResult; projectContext?: ProjectContext; rootUri?: vscode.Uri; flags?: Record<string, unknown> } =
92-
{ vscodeExtension: undefined, getIncludeFiles: undefined, projectContext: undefined, rootUri: undefined, flags: {} }
91+
{ vscodeExtension?: vscode.Extension<unknown>; getIncludeFiles?: GetIncludesResult; projectContext?: ProjectContext; rootUri?: vscode.Uri; flags?: Record<string, unknown> } =
92+
{ vscodeExtension: undefined, getIncludeFiles: undefined, projectContext: undefined, rootUri: undefined, flags: {} }
9393
) => {
9494
activeClientStub.getIncludes.resolves(getIncludeFiles);
9595
sinon.stub(lmTool, 'getProjectContext').resolves(projectContext);
9696
sinon.stub(activeClientStub, 'RootUri').get(() => rootUri);
97-
mockCopilotApi.registerRelatedFilesProvider.callsFake((_providerId: { extensionId: string; languageId: string }, callback: (uri: vscode.Uri, context: { flags: Record<string, unknown> }, cancellationToken: vscode.CancellationToken) => Promise<{ entries: vscode.Uri[]; traits?: CopilotTrait[] }>) => {
97+
mockCopilotApi.registerRelatedFilesProvider.callsFake((_providerId: { extensionId: string; languageId: string }, callback: (uri: vscode.Uri, context: { flags: Record<string, unknown> }, cancellationToken: vscode.CancellationToken) => Promise<{ entries: vscode.Uri[]; traits?: CopilotTrait[] } | undefined>) => {
9898
if (_providerId.languageId === 'cpp') {
9999
const tokenSource = new vscode.CancellationTokenSource();
100100
try {

0 commit comments

Comments
 (0)