1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const assert = require('assert');
25const exec = require('child_process').exec;
26const fixtures = require('../common/fixtures');
27
28function errExec(script, option, callback) {
29  callback = typeof option === 'function' ? option : callback;
30  option = typeof option === 'string' ? option : '';
31  const cmd = `"${process.argv[0]}" ${option} "${fixtures.path(script)}"`;
32  return exec(cmd, (err, stdout, stderr) => {
33    // There was some error
34    assert.ok(err);
35
36    // More than one line of error output.
37    assert.ok(stderr.split('\n').length);
38
39    // Proxy the args for more tests.
40    callback(err, stdout, stderr);
41  });
42}
43
44const syntaxErrorMessage = /\bSyntaxError\b/;
45
46
47// Simple throw error
48errExec('throws_error.js', common.mustCall((err, stdout, stderr) => {
49  assert.match(stderr, /blah/);
50}));
51
52
53// Trying to JSON.parse(undefined)
54errExec('throws_error2.js', common.mustCall((err, stdout, stderr) => {
55  assert.match(stderr, syntaxErrorMessage);
56}));
57
58
59// Trying to JSON.parse(undefined) in nextTick
60errExec('throws_error3.js', common.mustCall((err, stdout, stderr) => {
61  assert.match(stderr, syntaxErrorMessage);
62}));
63
64
65// throw ILLEGAL error
66errExec('throws_error4.js', common.mustCall((err, stdout, stderr) => {
67  assert.match(stderr, syntaxErrorMessage);
68}));
69
70// Specific long exception line doesn't result in stack overflow
71errExec('throws_error5.js', common.mustCall((err, stdout, stderr) => {
72  assert.match(stderr, syntaxErrorMessage);
73}));
74
75// Long exception line with length > errorBuffer doesn't result in assertion
76errExec('throws_error6.js', common.mustCall((err, stdout, stderr) => {
77  assert.match(stderr, syntaxErrorMessage);
78}));
79
80// Object that throws in toString() doesn't print garbage
81errExec('throws_error7.js', common.mustCall((err, stdout, stderr) => {
82  assert.match(stderr, /throw {\r?\n\^\r?\n{ toString: \[Function: toString] }\r?\n\r?\nNode\.js \S+\r?\n$/);
83}));
84
85// Regression tests for https://github.com/nodejs/node/issues/39149
86errExec('throws_error7.js', '--enable-source-maps', common.mustCall((err, stdout, stderr) => {
87  assert.match(stderr, /throw {\r?\n\^\r?\n{ toString: \[Function: toString] }\r?\n\r?\nNode\.js \S+\r?\n$/);
88}));
89