I have a simple example:
我有一个简单的例子:
var str = '{ "test" : 1, }'
try {
JSON.parse(str);
} catch (e) {
console.log(e)
}
result:
[SyntaxError: Unexpected token }]
[SyntaxError:意外的令牌}]
How to print all error info ?
如何打印所有错误信息?
Expected result:
undefined:1
{ "test" : 1, }
^
SyntaxError: Unexpected token }
2 个解决方案
#1
10
This will help:
这将有助于:
var x = { asd: "asd", };
try {
JSON.parse(x);
}
catch (e) {
console.log("Error", e.stack);
console.log("Error", e.name);
console.log("Error", e.message);
}
error.stack
is not exactly what you want, but it will help you.
error.stack并不是你想要的,但它会对你有所帮助。
#2
2
This will show you the various ways in which you can get the info available:
这将向您显示可以获得信息的各种方式:
var str = '{"test": 1, }';
try {
JSON.parse(str);
} catch(e) {
console.log("error object:");
console.log(e);
console.log();
console.log("error object toString():");
console.log("\t" + e.toString());
console.log();
console.log("error object attributes: ");
console.log('\tname: ' + e.name + ' message: ' + e.message + ' at: ' + e.at + ' text: ' + e.text);
console.log();
console.log("error object stack: ");
console.log(e.stack);
}
The output is:
输出是:
error object:
[SyntaxError: Unexpected token }]
error object toString():
SyntaxError: Unexpected token }
error object attributes:
name: SyntaxError message: Unexpected token } at: undefined text: undefined
error object stack:
SyntaxError: Unexpected token }
at Object.parse (native)
at Object.<anonymous> (/home/james/devel/tests/node/test.js:4:10)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
You can take your pick :-)
你可以选择:-)
Cheers,
James.
#1
10
This will help:
这将有助于:
var x = { asd: "asd", };
try {
JSON.parse(x);
}
catch (e) {
console.log("Error", e.stack);
console.log("Error", e.name);
console.log("Error", e.message);
}
error.stack
is not exactly what you want, but it will help you.
error.stack并不是你想要的,但它会对你有所帮助。
#2
2
This will show you the various ways in which you can get the info available:
这将向您显示可以获得信息的各种方式:
var str = '{"test": 1, }';
try {
JSON.parse(str);
} catch(e) {
console.log("error object:");
console.log(e);
console.log();
console.log("error object toString():");
console.log("\t" + e.toString());
console.log();
console.log("error object attributes: ");
console.log('\tname: ' + e.name + ' message: ' + e.message + ' at: ' + e.at + ' text: ' + e.text);
console.log();
console.log("error object stack: ");
console.log(e.stack);
}
The output is:
输出是:
error object:
[SyntaxError: Unexpected token }]
error object toString():
SyntaxError: Unexpected token }
error object attributes:
name: SyntaxError message: Unexpected token } at: undefined text: undefined
error object stack:
SyntaxError: Unexpected token }
at Object.parse (native)
at Object.<anonymous> (/home/james/devel/tests/node/test.js:4:10)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
You can take your pick :-)
你可以选择:-)
Cheers,
James.