Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  157] [ 2]  / answers: 1 / hits: 37206  / 13 Years ago, wed, april 27, 2011, 12:00:00

I'm using try catch on a node.js script:


try {} catch (err) {console.log(err)}

I get an output like this:


{ 
stack: [Getter/Setter],
arguments: [ 'undefined' ],
type: 'called_non_callable',
message: [Getter/Setter]
}

Is there an easy way to make this more informative? Include line numbers and function names and such?


More From » node.js

 Answers
17

Those [Getter/Setter] members indicate further information available on the error object. You can easily dump the contents of those getters/setters using a small helper function (very trivial implementation, further refinement is up to you)


function dumpError(err) {
if (typeof err === 'object') {
if (err.message) {
console.log('nMessage: ' + err.message)
}
if (err.stack) {
console.log('nStacktrace:')
console.log('====================')
console.log(err.stack);
}
} else {
console.log('dumpError :: argument is not an object');
}
}

try {
not_defined.function_call();
} catch(err) {
dumpError(err);
}

You could also extend the Object.prototype for improved accessibility (so you could use err.dumpError()), although extending Object.prototype bears the risk of overwriting existing functionality.


[#92534] Tuesday, April 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckenna

Total Points: 445
Total Questions: 109
Total Answers: 109

Location: Virgin Islands (U.S.)
Member since Sun, May 16, 2021
3 Years ago
;