|
| 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 | +} |
0 commit comments