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!
}
E dentro do_stuff
, ho:Come mai node.js non cattura i miei errori?
function do_stuff(){
//If I put the throw here, it will catch it!
insert_data('abc',function(){
throw new Error('haha');
});
}
Come mai non si è mai log 'errore catturato'? Invece, esso stampa lo stack-trace e l'oggetto di errore per schermo:
{ 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)
Si noti che se metto DESTRA DOPO lancio del do_stuff(), allora sarà prenderlo.
Come posso farlo catturare, anche se lo inserisco all'interno di un'altra funzione?
Qual è 'insert_data ('abc') {throw new Errore ('haha')}' dovrebbe essere? Questa non è una sintassi valida. Come è il tuo codice? – RightSaidFred
@RightSaidFred grazie, risolto. – TIMEX
@TIMEX non è possibile rilevare errori di ambienti asincroni, non funziona in questo modo. Smetti di usare 'try catch' – Raynos