summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/unit/test_error_to_exception.js
blob: 1330ecd0aeefb7d8d89941421717c215711b258e (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
/* 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/. */

add_task(async function() {
  // Throwing an error inside a JS callback in xpconnect should preserve
  // the columnNumber.

  const tests = [
    // Parser error.
    {
      throwError() {
        eval("a b");
      },
      messagePattern: /unexpected token/,
      lineNumber: 1,
      columnNumber: 3,
    },
    // Runtime error.
    {
      throwError() { // line = 21
        not_found();
      },
      messagePattern: /is not defined/,
      lineNumber: 22,
      columnNumber: 9,
    },
  ];

  for (const test of tests) {
    const { promise, resolve } = Promise.withResolvers();
    const listener = {
      observe(msg) {
        if (msg instanceof Ci.nsIScriptError) {
          resolve(msg);
        }
      }
    };

    try {
      Services.console.registerListener(listener);

      try {
        const obs = Cc["@mozilla.org/observer-service;1"]
              .getService(Ci.nsIObserverService);
        obs.addObserver(test.throwError, "test-obs", false);
        obs.notifyObservers(null, "test-obs");
      } catch {}

      const msg = await promise;
      Assert.stringMatches(msg.errorMessage, test.messagePattern);
      Assert.equal(msg.lineNumber, test.lineNumber);
      Assert.equal(msg.columnNumber, test.columnNumber);
    } finally {
      Services.console.unregisterListener(listener);
    }
  }
});