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, callback) { 29 const cmd = `"${process.argv[0]}" "${fixtures.path(script)}"`; 30 return exec(cmd, (err, stdout, stderr) => { 31 // There was some error 32 assert.ok(err); 33 34 // More than one line of error output. 35 assert.ok(stderr.split('\n').length); 36 37 // Proxy the args for more tests. 38 callback(err, stdout, stderr); 39 }); 40} 41 42const syntaxErrorMessage = /\bSyntaxError\b/; 43 44 45// Simple throw error 46errExec('throws_error.js', common.mustCall((err, stdout, stderr) => { 47 assert.ok(/blah/.test(stderr)); 48})); 49 50 51// Trying to JSON.parse(undefined) 52errExec('throws_error2.js', common.mustCall((err, stdout, stderr) => { 53 assert.ok(syntaxErrorMessage.test(stderr)); 54})); 55 56 57// Trying to JSON.parse(undefined) in nextTick 58errExec('throws_error3.js', common.mustCall((err, stdout, stderr) => { 59 assert.ok(syntaxErrorMessage.test(stderr)); 60})); 61 62 63// throw ILLEGAL error 64errExec('throws_error4.js', common.mustCall((err, stdout, stderr) => { 65 assert.ok(syntaxErrorMessage.test(stderr)); 66})); 67 68// Specific long exception line doesn't result in stack overflow 69errExec('throws_error5.js', common.mustCall((err, stdout, stderr) => { 70 assert.ok(syntaxErrorMessage.test(stderr)); 71})); 72 73// Long exception line with length > errorBuffer doesn't result in assertion 74errExec('throws_error6.js', common.mustCall((err, stdout, stderr) => { 75 assert.ok(syntaxErrorMessage.test(stderr)); 76})); 77 78// Object that throws in toString() doesn't print garbage 79errExec('throws_error7.js', common.mustCall((err, stdout, stderr) => { 80 assert.ok(/throw {\r?\n\^\r?\n{ toString: \[Function: toString] }\r?\n$/.test(stderr)); 81})); 82