Skip to content

Commit 5360f03

Browse files
authored
Merge pull request #2396 from github/koesie10/suppress-error-message
Suppress similar error messages when monitor MRVA
2 parents 14eb7ff + 5f34664 commit 5360f03

File tree

2 files changed

+110
-7
lines changed

2 files changed

+110
-7
lines changed

extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { sleep } from "../pure/time";
1414
import { getErrorMessage } from "../pure/helpers-pure";
1515
import { showAndLogWarningMessage } from "../helpers";
1616
import { App } from "../common/app";
17+
import { extLogger } from "../common";
1718

1819
export class VariantAnalysisMonitor extends DisposableObject {
1920
// With a sleep of 5 seconds, the maximum number of attempts takes
@@ -41,6 +42,8 @@ export class VariantAnalysisMonitor extends DisposableObject {
4142
let attemptCount = 0;
4243
const scannedReposDownloaded: number[] = [];
4344

45+
let lastErrorShown: string | undefined = undefined;
46+
4447
while (attemptCount <= VariantAnalysisMonitor.maxAttemptCount) {
4548
await sleep(VariantAnalysisMonitor.sleepTime);
4649

@@ -56,13 +59,22 @@ export class VariantAnalysisMonitor extends DisposableObject {
5659
variantAnalysis.id,
5760
);
5861
} catch (e) {
59-
void showAndLogWarningMessage(
60-
`Error while monitoring variant analysis ${
61-
variantAnalysis.query.name
62-
} (${variantAnalysis.query.language}) [${new Date(
63-
variantAnalysis.executionStartTime,
64-
).toLocaleString(env.language)}]: ${getErrorMessage(e)}`,
65-
);
62+
const errorMessage = getErrorMessage(e);
63+
64+
const message = `Error while monitoring variant analysis ${
65+
variantAnalysis.query.name
66+
} (${variantAnalysis.query.language}) [${new Date(
67+
variantAnalysis.executionStartTime,
68+
).toLocaleString(env.language)}]: ${errorMessage}`;
69+
70+
// If we have already shown this error to the user, don't show it again.
71+
if (lastErrorShown === errorMessage) {
72+
void extLogger.log(message);
73+
} else {
74+
void showAndLogWarningMessage(message);
75+
lastErrorShown = errorMessage;
76+
}
77+
6678
continue;
6779
}
6880

@@ -84,6 +96,9 @@ export class VariantAnalysisMonitor extends DisposableObject {
8496
}
8597

8698
attemptCount++;
99+
100+
// Reset the last error shown if we have successfully retrieved the variant analysis.
101+
lastErrorShown = undefined;
87102
}
88103
}
89104

extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-monitor.test.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import { createMockVariantAnalysis } from "../../../factories/variant-analysis/shared/variant-analysis";
2323
import { createMockApp } from "../../../__mocks__/appMock";
2424
import { createMockCommandManager } from "../../../__mocks__/commandsMock";
25+
import * as helpers from "../../../../src/helpers";
2526

2627
jest.setTimeout(60_000);
2728

@@ -196,6 +197,93 @@ describe("Variant Analysis Monitor", () => {
196197
});
197198
});
198199

200+
describe("when some responses fail", () => {
201+
let showAndLogWarningMessageSpy: jest.SpiedFunction<
202+
typeof helpers.showAndLogWarningMessage
203+
>;
204+
205+
let scannedRepos: ApiVariantAnalysisScannedRepository[];
206+
207+
beforeEach(async () => {
208+
showAndLogWarningMessageSpy = jest
209+
.spyOn(helpers, "showAndLogWarningMessage")
210+
.mockResolvedValue(undefined);
211+
212+
scannedRepos = createMockScannedRepos([
213+
"pending",
214+
"in_progress",
215+
"in_progress",
216+
"in_progress",
217+
"pending",
218+
"pending",
219+
]);
220+
mockApiResponse = createMockApiResponse("in_progress", scannedRepos);
221+
mockGetVariantAnalysis.mockResolvedValueOnce(mockApiResponse);
222+
223+
mockGetVariantAnalysis.mockRejectedValueOnce(
224+
new Error("No internet connection"),
225+
);
226+
mockGetVariantAnalysis.mockRejectedValueOnce(
227+
new Error("No internet connection"),
228+
);
229+
mockGetVariantAnalysis.mockRejectedValueOnce(
230+
new Error("My different error"),
231+
);
232+
233+
let nextApiResponse = {
234+
...mockApiResponse,
235+
scanned_repositories: [...scannedRepos.map((r) => ({ ...r }))],
236+
};
237+
nextApiResponse.scanned_repositories[0].analysis_status = "succeeded";
238+
nextApiResponse.scanned_repositories[1].analysis_status = "succeeded";
239+
240+
mockGetVariantAnalysis.mockResolvedValueOnce(nextApiResponse);
241+
242+
mockGetVariantAnalysis.mockRejectedValueOnce(
243+
new Error("My different error"),
244+
);
245+
mockGetVariantAnalysis.mockRejectedValueOnce(
246+
new Error("My different error"),
247+
);
248+
mockGetVariantAnalysis.mockRejectedValueOnce(
249+
new Error("Another different error"),
250+
);
251+
252+
nextApiResponse = {
253+
...mockApiResponse,
254+
scanned_repositories: [...scannedRepos.map((r) => ({ ...r }))],
255+
};
256+
nextApiResponse.scanned_repositories[2].analysis_status = "succeeded";
257+
nextApiResponse.scanned_repositories[3].analysis_status = "succeeded";
258+
nextApiResponse.scanned_repositories[4].analysis_status = "failed";
259+
nextApiResponse.scanned_repositories[5].analysis_status = "succeeded";
260+
nextApiResponse.status = "succeeded";
261+
mockGetVariantAnalysis.mockResolvedValueOnce(nextApiResponse);
262+
});
263+
264+
it("should only trigger the warning once per error", async () => {
265+
await variantAnalysisMonitor.monitorVariantAnalysis(variantAnalysis);
266+
267+
expect(showAndLogWarningMessageSpy).toBeCalledTimes(4);
268+
expect(showAndLogWarningMessageSpy).toHaveBeenNthCalledWith(
269+
1,
270+
expect.stringMatching(/No internet connection/),
271+
);
272+
expect(showAndLogWarningMessageSpy).toHaveBeenNthCalledWith(
273+
2,
274+
expect.stringMatching(/My different error/),
275+
);
276+
expect(showAndLogWarningMessageSpy).toHaveBeenNthCalledWith(
277+
3,
278+
expect.stringMatching(/My different error/),
279+
);
280+
expect(showAndLogWarningMessageSpy).toHaveBeenNthCalledWith(
281+
4,
282+
expect.stringMatching(/Another different error/),
283+
);
284+
});
285+
});
286+
199287
describe("when there are no repos to scan", () => {
200288
beforeEach(async () => {
201289
scannedRepos = [];

0 commit comments

Comments
 (0)