summaryrefslogtreecommitdiff
path: root/jstests/core/mr_tolerates_js_exception.js
blob: e291639cb92e0fb0301dc6740b97e8270bb15479 (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
/**
 * Test that the mapReduce command fails gracefully when user-provided JavaScript code throws and
 * that the user gets back a JavaScript stacktrace.
 *
 * @tags: [
 *   # mapReduce does not support afterClusterTime.
 *   does_not_support_causal_consistency,
 *   does_not_support_stepdowns,
 *   requires_scripting,
 * ]
 */
(function() {
"use strict";

let coll = db.mr_tolerates_js_exception;
coll.drop();
for (let i = 0; i < 100; i++) {
    assert.commandWorked(coll.insert({_id: i, a: 1}));
}

// Test that the command fails with a JS interpreter failure error when the reduce function
// throws.
let cmdOutput = db.runCommand({
    mapReduce: coll.getName(),
    map: function() {
        emit(this.a, 1);
    },
    reduce: function(key, value) {
        (function myFunction() {
            throw new Error("Intentionally thrown inside reduce function");
        })();
    },
    out: {inline: 1}
});
assert.commandFailedWithCode(cmdOutput, ErrorCodes.JSInterpreterFailure, tojson(cmdOutput));
assert(
    /Intentionally thrown inside reduce function/.test(cmdOutput.errmsg),
    () => "mapReduce didn't include the message from the exception thrown: " + tojson(cmdOutput));
assert(/myFunction@/.test(cmdOutput.errmsg),
       () => "mapReduce didn't return the JavaScript stacktrace: " + tojson(cmdOutput));
assert(!cmdOutput.hasOwnProperty("stack"),
       () => "mapReduce shouldn't return JavaScript stacktrace separately: " + tojson(cmdOutput));
assert(!cmdOutput.hasOwnProperty("originalError"),
       () => "mapReduce shouldn't return wrapped version of the error: " + tojson(cmdOutput));

// Test that the command fails with a JS interpreter failure error when the map function
// throws.
cmdOutput = db.runCommand({
    mapReduce: coll.getName(),
    map: function() {
        (function myFunction() {
            throw new Error("Intentionally thrown inside map function");
        })();
    },
    reduce: function(key, value) {
        return Array.sum(value);
    },
    out: {inline: 1}
});
assert.commandFailedWithCode(cmdOutput, ErrorCodes.JSInterpreterFailure, tojson(cmdOutput));
assert(
    /Intentionally thrown inside map function/.test(cmdOutput.errmsg),
    () => "mapReduce didn't include the message from the exception thrown: " + tojson(cmdOutput));
assert(/myFunction@/.test(cmdOutput.errmsg),
       () => "mapReduce didn't return the JavaScript stacktrace: " + tojson(cmdOutput));
assert(!cmdOutput.hasOwnProperty("stack"),
       () => "mapReduce shouldn't return JavaScript stacktrace separately: " + tojson(cmdOutput));
assert(!cmdOutput.hasOwnProperty("originalError"),
       () => "mapReduce shouldn't return wrapped version of the error: " + tojson(cmdOutput));
}());