Skip to content

Commit 8df6f9e

Browse files
committed
Close nodejs#974 Properly report traceless errors.
Also, tests for the same.
1 parent 8fd1c68 commit 8df6f9e

File tree

7 files changed

+125
-3
lines changed

7 files changed

+125
-3
lines changed

src/node.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,13 +1274,24 @@ static void ReportException(TryCatch &try_catch, bool show_line) {
12741274

12751275
String::Utf8Value trace(try_catch.StackTrace());
12761276

1277-
if (trace.length() > 0) {
1277+
// range errors have a trace member set to undefined
1278+
if (trace.length() > 0 && !try_catch.StackTrace()->IsUndefined()) {
12781279
fprintf(stderr, "%s\n", *trace);
12791280
} else {
12801281
// this really only happens for RangeErrors, since they're the only
1281-
// kind that won't have all this info in the trace.
1282+
// kind that won't have all this info in the trace, or when non-Error
1283+
// objects are thrown manually.
12821284
Local<Value> er = try_catch.Exception();
1283-
String::Utf8Value msg(!er->IsObject() ? er->ToString()
1285+
bool isErrorObject = er->IsObject() &&
1286+
!(er->ToObject()->Get(String::New("message"))->IsUndefined()) &&
1287+
!(er->ToObject()->Get(String::New("name"))->IsUndefined());
1288+
1289+
if (isErrorObject) {
1290+
String::Utf8Value name(er->ToObject()->Get(String::New("name")));
1291+
fprintf(stderr, "%s: ", *name);
1292+
}
1293+
1294+
String::Utf8Value msg(!isErrorObject ? er->ToString()
12841295
: er->ToObject()->Get(String::New("message"))->ToString());
12851296
fprintf(stderr, "%s\n", *msg);
12861297
}

test/message/stack_overflow.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
common.error('before');
26+
27+
// stack overflow
28+
function stackOverflow() {
29+
stackOverflow();
30+
}
31+
stackOverflow();
32+
33+
common.error('after');

test/message/stack_overflow.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
before
2+
3+
node.js:134
4+
throw e; // process.nextTick error, or 'error' event on first tick
5+
^
6+
RangeError: Maximum call stack size exceeded

test/message/throw_custom_error.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
common.error('before');
26+
27+
// custom error throwing
28+
throw { name: 'MyCustomError', message: 'This is a custom message' };
29+
30+
common.error('after');
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
before
2+
3+
node.js:134
4+
throw e; // process.nextTick error, or 'error' event on first tick
5+
^
6+
MyCustomError: This is a custom message

test/message/throw_non_error.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
common.error('before');
26+
27+
// custom error throwing
28+
throw { foo : 'bar' };
29+
30+
common.error('after');

test/message/throw_non_error.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
before
2+
3+
node.js:134
4+
throw e; // process.nextTick error, or 'error' event on first tick
5+
^
6+
[object Object]

0 commit comments

Comments
 (0)