Skip to content

Commit 1799426

Browse files
authored
Merge pull request #2506 from github/koesie10/split-helpers-3
Move show and log functions out of helpers
2 parents c6c5628 + f67f53d commit 1799426

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+242
-222
lines changed

extensions/ql-vscode/src/codeql-cli/distribution.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import * as semver from "semver";
66
import { URL } from "url";
77
import { ExtensionContext, Event } from "vscode";
88
import { DistributionConfig } from "../config";
9-
import { showAndLogErrorMessage, showAndLogWarningMessage } from "../helpers";
109
import { extLogger } from "../common";
1110
import { getCodeQlCliVersion } from "./cli-version";
1211
import {
@@ -23,6 +22,10 @@ import {
2322
InvocationRateLimiter,
2423
InvocationRateLimiterResultKind,
2524
} from "../common/invocation-rate-limiter";
25+
import {
26+
showAndLogErrorMessage,
27+
showAndLogWarningMessage,
28+
} from "../common/vscode/log";
2629

2730
/**
2831
* distribution.ts

extensions/ql-vscode/src/common/vscode/commands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {
88
} from "../../pure/helpers-pure";
99
import { redactableError } from "../../pure/errors";
1010
import { UserCancellationException } from "./progress";
11+
import { telemetryListener } from "../../telemetry";
1112
import {
1213
showAndLogExceptionWithTelemetry,
1314
showAndLogWarningMessage,
14-
} from "../../helpers";
15-
import { telemetryListener } from "../../telemetry";
15+
} from "./log";
1616

1717
/**
1818
* Create a command manager for VSCode, wrapping registerCommandWithErrorHandling

extensions/ql-vscode/src/common/vscode/external-files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Uri, window } from "vscode";
22
import { AppCommandManager } from "../commands";
3-
import { showAndLogExceptionWithTelemetry } from "../../helpers";
43
import { showBinaryChoiceDialog } from "./dialog";
54
import { redactableError } from "../../pure/errors";
65
import {
76
asError,
87
getErrorMessage,
98
getErrorStack,
109
} from "../../pure/helpers-pure";
10+
import { showAndLogExceptionWithTelemetry } from "./log";
1111

1212
export async function tryOpenExternalFile(
1313
commandManager: AppCommandManager,
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { window } from "vscode";
2+
import { RedactableError } from "../../pure/errors";
3+
import { telemetryListener } from "../../telemetry";
4+
import { extLogger, OutputChannelLogger } from "../logging";
5+
6+
interface ShowAndLogExceptionOptions extends ShowAndLogOptions {
7+
/** Custom properties to include in the telemetry report. */
8+
extraTelemetryProperties?: { [key: string]: string };
9+
}
10+
11+
interface ShowAndLogOptions {
12+
/** The output logger that will receive the message. */
13+
outputLogger?: OutputChannelLogger;
14+
/** A set of items that will be rendered as actions in the message. */
15+
items?: string[];
16+
/**
17+
* An alternate message that is added to the log, but not displayed in the popup.
18+
* This is useful for adding extra detail to the logs that would be too noisy for the popup.
19+
*/
20+
fullMessage?: string;
21+
}
22+
23+
/**
24+
* Show an error message, log it to the console, and emit redacted information as telemetry
25+
*
26+
* @param error The error to show. Only redacted information will be included in the telemetry.
27+
* @param options See individual fields on `ShowAndLogExceptionOptions` type.
28+
*
29+
* @return A promise that resolves to the selected item or undefined when being dismissed.
30+
*/
31+
export async function showAndLogExceptionWithTelemetry(
32+
error: RedactableError,
33+
options: ShowAndLogExceptionOptions = {},
34+
): Promise<string | undefined> {
35+
telemetryListener?.sendError(error, options.extraTelemetryProperties);
36+
return showAndLogErrorMessage(error.fullMessage, options);
37+
}
38+
39+
/**
40+
* Show an error message and log it to the console
41+
*
42+
* @param message The message to show.
43+
* @param options See individual fields on `ShowAndLogOptions` type.
44+
*
45+
* @return A promise that resolves to the selected item or undefined when being dismissed.
46+
*/
47+
export async function showAndLogErrorMessage(
48+
message: string,
49+
options?: ShowAndLogOptions,
50+
): Promise<string | undefined> {
51+
return internalShowAndLog(
52+
dropLinesExceptInitial(message),
53+
window.showErrorMessage,
54+
{ fullMessage: message, ...options },
55+
);
56+
}
57+
58+
function dropLinesExceptInitial(message: string, n = 2) {
59+
return message.toString().split(/\r?\n/).slice(0, n).join("\n");
60+
}
61+
62+
/**
63+
* Show a warning message and log it to the console
64+
*
65+
* @param message The message to show.
66+
* @param options See individual fields on `ShowAndLogOptions` type.
67+
*
68+
* @return A promise that resolves to the selected item or undefined when being dismissed.
69+
*/
70+
export async function showAndLogWarningMessage(
71+
message: string,
72+
options?: ShowAndLogOptions,
73+
): Promise<string | undefined> {
74+
return internalShowAndLog(message, window.showWarningMessage, options);
75+
}
76+
77+
/**
78+
* Show an information message and log it to the console
79+
*
80+
* @param message The message to show.
81+
* @param options See individual fields on `ShowAndLogOptions` type.
82+
*
83+
* @return A promise that resolves to the selected item or undefined when being dismissed.
84+
*/
85+
export async function showAndLogInformationMessage(
86+
message: string,
87+
options?: ShowAndLogOptions,
88+
): Promise<string | undefined> {
89+
return internalShowAndLog(message, window.showInformationMessage, options);
90+
}
91+
92+
type ShowMessageFn = (
93+
message: string,
94+
...items: string[]
95+
) => Thenable<string | undefined>;
96+
97+
async function internalShowAndLog(
98+
message: string,
99+
fn: ShowMessageFn,
100+
{ items = [], outputLogger = extLogger, fullMessage }: ShowAndLogOptions = {},
101+
): Promise<string | undefined> {
102+
const label = "Show Log";
103+
void outputLogger.log(fullMessage || message);
104+
const result = await fn(message, label, ...items);
105+
if (result === label) {
106+
outputLogger.show();
107+
}
108+
return result;
109+
}

extensions/ql-vscode/src/common/vscode/selection-commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { showAndLogErrorMessage } from "../../helpers";
21
import {
32
ExplorerSelectionCommandFunction,
43
TreeViewContextMultiSelectionCommandFunction,
54
TreeViewContextSingleSelectionCommandFunction,
65
} from "../commands";
6+
import { showAndLogErrorMessage } from "./log";
77

88
// A hack to match types that are not an array, which is useful to help avoid
99
// misusing createSingleSelectionCommand, e.g. where T accidentally gets instantiated

extensions/ql-vscode/src/compare/compare-view.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import {
2424
} from "../common/vscode/abstract-webview";
2525
import { telemetryListener } from "../telemetry";
2626
import { redactableError } from "../pure/errors";
27-
import { showAndLogExceptionWithTelemetry } from "../helpers";
27+
28+
import { showAndLogExceptionWithTelemetry } from "../common/vscode/log";
2829

2930
interface ComparePair {
3031
from: CompletedLocalQueryInfo;

extensions/ql-vscode/src/data-extensions-editor/data-extensions-editor-module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { DatabaseManager } from "../databases/local-databases";
77
import { ensureDir } from "fs-extra";
88
import { join } from "path";
99
import { App } from "../common/app";
10-
import { showAndLogErrorMessage } from "../helpers";
1110
import { withProgress } from "../common/vscode/progress";
1211
import { pickExtensionPackModelFile } from "./extension-pack-picker";
12+
import { showAndLogErrorMessage } from "../common/vscode/log";
1313

1414
const SUPPORTED_LANGUAGES: string[] = ["java", "csharp"];
1515

extensions/ql-vscode/src/data-extensions-editor/data-extensions-editor-view.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ import {
1717
} from "../pure/interface-types";
1818
import { ProgressUpdate } from "../common/vscode/progress";
1919
import { QueryRunner } from "../query-server";
20-
import {
21-
showAndLogErrorMessage,
22-
showAndLogExceptionWithTelemetry,
23-
} from "../helpers";
2420
import { extLogger } from "../common";
2521
import { outputFile, pathExists, readFile } from "fs-extra";
2622
import { load as loadYaml } from "js-yaml";
@@ -46,6 +42,10 @@ import {
4642
} from "./auto-model";
4743
import { showLlmGeneration } from "../config";
4844
import { getAutoModelUsages } from "./auto-model-usages-query";
45+
import {
46+
showAndLogErrorMessage,
47+
showAndLogExceptionWithTelemetry,
48+
} from "../common/vscode/log";
4949

5050
export class DataExtensionsEditorView extends AbstractWebview<
5151
ToDataExtensionsEditorMessage,

extensions/ql-vscode/src/data-extensions-editor/extension-pack-picker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { dump as dumpYaml, load as loadYaml } from "js-yaml";
44
import { minimatch } from "minimatch";
55
import { CancellationToken, window } from "vscode";
66
import { CodeQLCliServer } from "../codeql-cli/cli";
7-
import { showAndLogErrorMessage } from "../helpers";
87
import {
98
getOnDiskWorkspaceFolders,
109
getOnDiskWorkspaceFoldersObjects,
@@ -14,6 +13,7 @@ import { DatabaseItem } from "../databases/local-databases";
1413
import { getQlPackPath, QLPACK_FILENAMES } from "../pure/ql";
1514
import { getErrorMessage } from "../pure/helpers-pure";
1615
import { ExtensionPack, ExtensionPackModelFile } from "./shared/extension-pack";
16+
import { showAndLogErrorMessage } from "../common/vscode/log";
1717
import { containsPath } from "../pure/files";
1818

1919
const maxStep = 3;

extensions/ql-vscode/src/data-extensions-editor/external-api-usage-query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { CoreCompletedQuery, QueryRunner } from "../query-server";
22
import { dir } from "tmp-promise";
33
import { writeFile } from "fs-extra";
44
import { dump as dumpYaml } from "js-yaml";
5-
import { showAndLogExceptionWithTelemetry } from "../helpers";
65
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
76
import { TeeLogger } from "../common";
87
import { isQueryLanguage } from "../common/query-language";
@@ -14,6 +13,7 @@ import { fetchExternalApiQueries } from "./queries";
1413
import { QueryResultType } from "../pure/new-messages";
1514
import { join } from "path";
1615
import { redactableError } from "../pure/errors";
16+
import { showAndLogExceptionWithTelemetry } from "../common/vscode/log";
1717

1818
export type RunQueryOptions = {
1919
cliServer: Pick<CodeQLCliServer, "resolveQlpacks">;

0 commit comments

Comments
 (0)