summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/js_exceptions.js
blob: b54b5f35a269049fa1c69f729d94131d945bf143 (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
/**
 * Validate that exceptions are reported correctly
 *
 */
(function() {
'use strict';

let tests = [
    {
        callback: function() {
            UUID("asdf");
        },
        match: "Error: Invalid UUID string: asdf :",
        stack: true,
    },
    {
        callback: function() {
            throw {};
        },
        match: "uncaught exception: \\\[object Object\\\] :",
        stack: undefined,
    },
    {
        callback: function() {
            throw "asdf";
        },
        match: "uncaught exception: asdf",
        stack: false,
    },
    {
        callback: function() {
            throw 1;
        },
        match: "uncaught exception: 1",
        stack: false,
    },
    {
        callback: function() {
            foo.bar();
        },
        match: "uncaught exception: ReferenceError: foo is not defined :",
        stack: true,
    },
    {
        callback: function() {
            throw function() {};
        },
        match: "function\\\(\\\) {} :",
        stack: undefined,
    },
    {
        callback: function() {
            try {
                UUID("asdf");
            } catch (e) {
                throw (e.constructor());
            }
        },
        match: "uncaught exception: Error :",
        stack: true,
    },
    {
        callback: function() {
            try {
                UUID("asdf");
            } catch (e) {
                throw (e.prototype);
            }
        },
        match: "uncaught exception: undefined",
        stack: false,
    },
];
function recurser(depth, limit, callback) {
    if (++depth >= limit) {
        callback();
    } else {
        recurser(depth, limit, callback);
    }
}
function assertMatch(m, l) {
    assert(m.test(l), m + " didn't match \"" + l + "\"");
}
tests.forEach(function(t) {
    let code = tojson(recurser);
    [1, 2, 10].forEach(function(depth) {
        clearRawMongoProgramOutput();
        assert.throws(startParallelShell(
            code + ";\nrecurser(0," + depth + "," + tojson(t.callback) + ");", false, true));
        let output = rawMongoProgramOutput();
        let lines = output.split(/\s*\n|\\n/);
        let matchShellExp = false;
        while (lines.length > 0 & matchShellExp !== true) {
            let line = lines.shift();
            if (line.match(/MongoDB shell version/)) {
                matchShellExp = true;
            }
        }
        assert(matchShellExp);
        assertMatch(/\s*/, lines.pop());
        assertMatch(/exiting with code/, lines.pop());
        assertMatch(new RegExp(t.match), lines.shift());

        if (t.stack == true) {
            assert.eq(lines.length,
                      depth + 2);  // plus one for the shell and one for the callback
            lines.forEach(function(l) {
                assertMatch(/\@\(shell eval\):\d+:\d+/, l);
            });
            lines.pop();
            lines.shift();
            lines.forEach(function(l) {
                assertMatch(/recurser\@/, l);
            });
        } else if (t.stack == false) {
            assert.eq(lines.length, 0);
        } else if (t.stack == undefined) {
            assert.eq(lines.length, 1);
            assertMatch(/undefined/, lines.pop());
        }
    });
});
})();