69 lines · typescript
1import * as vscode from "vscode";2import {3 ConfigureButton,4 NotificationButton,5 showErrorMessage,6} from "./show-error-message";7 8/** Options used to configure {@link ErrorWithNotification.showNotification}. */9export interface ShowNotificationOptions extends vscode.MessageOptions {10 /**11 * Whether or not to show the configure launch configuration button.12 *13 * **IMPORTANT**: the configure launch configuration button will do nothing if the14 * callee isn't a {@link vscode.DebugConfigurationProvider}.15 */16 showConfigureButton?: boolean;17}18 19/**20 * An error that is able to be displayed to the user as a notification.21 *22 * Used in combination with {@link showErrorMessage showErrorMessage()} when whatever caused23 * the error was the result of a direct action by the user. E.g. launching a debug session.24 */25export class ErrorWithNotification extends Error {26 private readonly buttons: NotificationButton<any, null | undefined>[];27 28 constructor(29 message: string,30 ...buttons: NotificationButton<any, null | undefined>[]31 ) {32 super(message);33 this.buttons = buttons;34 }35 36 /**37 * Shows the notification to the user including the configure launch configuration button.38 *39 * **IMPORTANT**: the configure launch configuration button will do nothing if the40 * callee isn't a {@link vscode.DebugConfigurationProvider}.41 *42 * @param options Configure the behavior of the notification43 */44 showNotification(45 options: ShowNotificationOptions & { showConfigureButton: true },46 ): Promise<null | undefined>;47 48 /**49 * Shows the notification to the user.50 *51 * @param options Configure the behavior of the notification52 */53 showNotification(options?: ShowNotificationOptions): Promise<undefined>;54 55 // Actual implementation of showNotification()56 async showNotification(57 options: ShowNotificationOptions = {},58 ): Promise<null | undefined> {59 // Filter out the configure button unless explicitly requested60 let buttons = this.buttons;61 if (options.showConfigureButton !== true) {62 buttons = buttons.filter(63 (button) => !(button instanceof ConfigureButton),64 );65 }66 return showErrorMessage(this.message, options, ...buttons);67 }68}69