summaryrefslogtreecommitdiffstats
path: root/browser/extensions/screenshots/background/senderror.js
blob: 3d5eae5ec6e99d4c93a2508d4408cf0fbaf41480 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

/* globals startBackground, analytics, communication, catcher, log, browser, getStrings */

"use strict";

this.senderror = (function () {
  const exports = {};

  // Do not show an error more than every ERROR_TIME_LIMIT milliseconds:
  const ERROR_TIME_LIMIT = 3000;

  const messages = {
    REQUEST_ERROR: {
      titleKey: "screenshots-request-error-title",
      infoKey: "screenshots-request-error-details",
    },
    CONNECTION_ERROR: {
      titleKey: "screenshots-connection-error-title",
      infoKey: "screenshots-connection-error-details",
    },
    LOGIN_ERROR: {
      titleKey: "screenshots-request-error-title",
      infoKey: "screenshots-login-error-details",
    },
    LOGIN_CONNECTION_ERROR: {
      titleKey: "screenshots-connection-error-title",
      infoKey: "screenshots-connection-error-details",
    },
    UNSHOOTABLE_PAGE: {
      titleKey: "screenshots-unshootable-page-error-title",
      infoKey: "screenshots-unshootable-page-error-details",
    },
    EMPTY_SELECTION: {
      titleKey: "screenshots-empty-selection-error-title",
    },
    PRIVATE_WINDOW: {
      titleKey: "screenshots-private-window-error-title",
      infoKey: "screenshots-private-window-error-details",
    },
    generic: {
      titleKey: "screenshots-generic-error-title",
      infoKey: "screenshots-generic-error-details",
      showMessage: true,
    },
  };

  communication.register("reportError", (sender, error) => {
    catcher.unhandled(error);
  });

  let lastErrorTime;

  exports.showError = async function (error) {
    if (lastErrorTime && Date.now() - lastErrorTime < ERROR_TIME_LIMIT) {
      return;
    }
    lastErrorTime = Date.now();
    const id = crypto.randomUUID();
    let popupMessage = error.popupMessage || "generic";
    if (!messages[popupMessage]) {
      popupMessage = "generic";
    }

    let item = messages[popupMessage];
    if (!("title" in item)) {
      let keys = [{ id: item.titleKey }];
      if ("infoKey" in item) {
        keys.push({ id: item.infoKey });
      }

      [item.title, item.info] = await getStrings(keys);
    }

    let title = item.title;
    let message = item.info || "";
    const showMessage = item.showMessage;
    if (error.message && showMessage) {
      if (message) {
        message += "\n" + error.message;
      } else {
        message = error.message;
      }
    }
    if (Date.now() - startBackground.startTime > 5 * 1000) {
      browser.notifications.create(id, {
        type: "basic",
        // FIXME: need iconUrl for an image, see #2239
        title,
        message,
      });
    }
  };

  exports.reportError = function (e) {
    if (!analytics.isTelemetryEnabled()) {
      log.error("Telemetry disabled. Not sending critical error:", e);
      return;
    }
    const exception = new Error(e.message);
    exception.stack = e.multilineStack || e.stack || undefined;

    // To improve Sentry reporting & grouping, replace the
    // moz-extension://$uuid base URL with a generic resource:// URL.
    if (exception.stack) {
      exception.stack = exception.stack.replace(
        /moz-extension:\/\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/g,
        "resource://screenshots-addon"
      );
    }
    const rest = {};
    for (const attr in e) {
      if (
        ![
          "name",
          "message",
          "stack",
          "multilineStack",
          "popupMessage",
          "version",
          "sentryPublicDSN",
          "help",
          "fromMakeError",
        ].includes(attr)
      ) {
        rest[attr] = e[attr];
      }
    }
    rest.stack = exception.stack;
  };

  catcher.registerHandler(errorObj => {
    if (!errorObj.noPopup) {
      exports.showError(errorObj);
    }
    if (!errorObj.noReport) {
      exports.reportError(errorObj);
    }
  });

  return exports;
})();