var api_friends_helper = require('./helper.js');
try{
api_friends_helper.do_stuff(function(result){
console.log('success');
};
}catch(err){
console.log('caught error'); //this doesn't hit!
}
और do_stuff
अंदर, मेरे पास है:कैसे node.js मेरी त्रुटियों को पकड़ नहीं आता है?
function do_stuff(){
//If I put the throw here, it will catch it!
insert_data('abc',function(){
throw new Error('haha');
});
}
कैसे आ यह कभी नहीं लॉग के त्रुटि पकड़ लिया? इसके बजाय, यह स्क्रीन करने के लिए ढेर का पता लगाने और त्रुटि वस्तु प्रिंट:
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'haha' }
Error: haha
at /home/abc/kj/src/api/friends/helper.js:18:23
at /home/abc/kj/src/api/friends/db.js:44:13
at Query.<anonymous> (/home/abc/kj/src/node_modules/mysql/lib/client.js:108:11)
at Query.emit (events.js:61:17)
at Query._handlePacket (/home/abc/kj/src/node_modules/mysql/lib/query.js:51:14)
at Client._handlePacket (/home/abc/kj/src/node_modules/mysql/lib/client.js:312:14)
at Parser.<anonymous> (native)
at Parser.emit (events.js:64:17)
at /home/abc/kj/src/node_modules/mysql/lib/parser.js:71:14
at Parser.write (/home/abc/kj/src/node_modules/mysql/lib/parser.js:576:7)
सूचना है कि अगर मैं) do_stuff (के बाद फेंक सही शब्दों में कहें, तो यह इसके शिकार हो सकते।
मैं इसे कैसे पकड़ सकता हूं, भले ही मैं इसे किसी अन्य फ़ंक्शन के अंदर घोंसला दूं?
क्या 'insert_data ('एबीसी') है बनाने {नई त्रुटि फेंक ('haha')}' होना चाहिए? यह वैध वाक्यविन्यास नहीं है। आपका कोड वास्तव में कैसा दिखता है? – RightSaidFred
@RightSaidFred धन्यवाद, तय। – TIMEX
@TIMEX आप एसिंक्रोनस वातावरण की त्रुटियों को नहीं पकड़ सकते हैं, यह इस तरह से काम नहीं करता है। 'कोशिश करें' का उपयोग करना बंद करें – Raynos