aboutsummaryrefslogtreecommitdiff
path: root/lldb/tools/lldb-dap/src-ts/ui/error-with-notification.ts
blob: 1f8676d3eb13543611d871158e53c897c3de56f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import * as vscode from "vscode";
import {
  ConfigureButton,
  NotificationButton,
  showErrorMessage,
} from "./show-error-message";

/** Options used to configure {@link ErrorWithNotification.showNotification}. */
export interface ShowNotificationOptions extends vscode.MessageOptions {
  /**
   * Whether or not to show the configure launch configuration button.
   *
   * **IMPORTANT**: the configure launch configuration button will do nothing if the
   * callee isn't a {@link vscode.DebugConfigurationProvider}.
   */
  showConfigureButton?: boolean;
}

/**
 * An error that is able to be displayed to the user as a notification.
 *
 * Used in combination with {@link showErrorMessage showErrorMessage()} when whatever caused
 * the error was the result of a direct action by the user. E.g. launching a debug session.
 */
export class ErrorWithNotification extends Error {
  private readonly buttons: NotificationButton<any, null | undefined>[];

  constructor(
    message: string,
    ...buttons: NotificationButton<any, null | undefined>[]
  ) {
    super(message);
    this.buttons = buttons;
  }

  /**
   * Shows the notification to the user including the configure launch configuration button.
   *
   * **IMPORTANT**: the configure launch configuration button will do nothing if the
   * callee isn't a {@link vscode.DebugConfigurationProvider}.
   *
   * @param options Configure the behavior of the notification
   */
  showNotification(
    options: ShowNotificationOptions & { showConfigureButton: true },
  ): Promise<null | undefined>;

  /**
   * Shows the notification to the user.
   *
   * @param options Configure the behavior of the notification
   */
  showNotification(options?: ShowNotificationOptions): Promise<undefined>;

  // Actual implementation of showNotification()
  async showNotification(
    options: ShowNotificationOptions = {},
  ): Promise<null | undefined> {
    // Filter out the configure button unless explicitly requested
    let buttons = this.buttons;
    if (options.showConfigureButton !== true) {
      buttons = buttons.filter(
        (button) => !(button instanceof ConfigureButton),
      );
    }
    return showErrorMessage(this.message, options, ...buttons);
  }
}