|
| 1 | +import { |
| 2 | + showAndLogWarningMessage, |
| 3 | + showAndLogExceptionWithTelemetry, |
| 4 | +} from "../logging"; |
| 5 | +import type { NotificationLogger } from "../logging"; |
| 6 | +import { extLogger } from "../logging/vscode"; |
| 7 | +import type { AppTelemetry } from "../telemetry"; |
| 8 | +import { telemetryListener } from "./telemetry"; |
| 9 | +import { asError, getErrorMessage } from "../helpers-pure"; |
| 10 | +import { redactableError } from "../errors"; |
| 11 | +import { UserCancellationException } from "./progress"; |
| 12 | +import { CliError } from "../../codeql-cli/cli-errors"; |
| 13 | +import { EOL } from "os"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Executes a task with error handling. It provides a uniform way to handle errors. |
| 17 | + * |
| 18 | + * @template T - A function type that takes an unknown number of arguments and returns a Promise. |
| 19 | + * @param {T} task - The task to be executed. |
| 20 | + * @param {NotificationLogger} [logger=extLogger] - The logger to use for error reporting. |
| 21 | + * @param {AppTelemetry | undefined} [telemetry=telemetryListener] - The telemetry listener to use for error reporting. |
| 22 | + * @param {string} [commandId] - The optional command id associated with the task. |
| 23 | + * @param {...unknown} args - The arguments to be passed to the task. |
| 24 | + * @returns {Promise<unknown>} The result of the task, or undefined if an error occurred. |
| 25 | + * @throws {Error} If an error occurs during the execution of the task. |
| 26 | + */ |
| 27 | +export async function runWithErrorHandling< |
| 28 | + T extends (...args: unknown[]) => Promise<unknown>, |
| 29 | +>( |
| 30 | + task: T, |
| 31 | + logger: NotificationLogger = extLogger, |
| 32 | + telemetry: AppTelemetry | undefined = telemetryListener, |
| 33 | + commandId?: string, |
| 34 | + ...args: unknown[] |
| 35 | +): Promise<unknown> { |
| 36 | + const startTime = Date.now(); |
| 37 | + let error: Error | undefined; |
| 38 | + |
| 39 | + try { |
| 40 | + return await task(...args); |
| 41 | + } catch (e) { |
| 42 | + error = asError(e); |
| 43 | + const errorMessage = redactableError(error)`${ |
| 44 | + getErrorMessage(e) || e |
| 45 | + }${commandId ? ` (${commandId})` : ""}`; |
| 46 | + |
| 47 | + const extraTelemetryProperties = commandId |
| 48 | + ? { command: commandId } |
| 49 | + : undefined; |
| 50 | + |
| 51 | + if (e instanceof UserCancellationException) { |
| 52 | + // User has cancelled this action manually |
| 53 | + if (e.silent) { |
| 54 | + void logger.log(errorMessage.fullMessage); |
| 55 | + } else { |
| 56 | + void showAndLogWarningMessage(logger, errorMessage.fullMessage); |
| 57 | + } |
| 58 | + } else if (e instanceof CliError) { |
| 59 | + const fullMessage = `${e.commandDescription} failed with args:${EOL} ${e.commandArgs.join(" ")}${EOL}${ |
| 60 | + e.stderr ?? e.cause |
| 61 | + }`; |
| 62 | + void showAndLogExceptionWithTelemetry(logger, telemetry, errorMessage, { |
| 63 | + fullMessage, |
| 64 | + extraTelemetryProperties, |
| 65 | + }); |
| 66 | + } else { |
| 67 | + // Include the full stack in the error log only. |
| 68 | + const fullMessage = errorMessage.fullMessageWithStack; |
| 69 | + void showAndLogExceptionWithTelemetry(logger, telemetry, errorMessage, { |
| 70 | + fullMessage, |
| 71 | + extraTelemetryProperties, |
| 72 | + }); |
| 73 | + } |
| 74 | + return undefined; |
| 75 | + } finally { |
| 76 | + if (commandId) { |
| 77 | + const executionTime = Date.now() - startTime; |
| 78 | + telemetryListener?.sendCommandUsage(commandId, executionTime, error); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments