V
V
Vadim2014-11-13 00:08:49
Node.js
Vadim, 2014-11-13 00:08:49

How do you catch and handle errors in nodejs?

For example, I connect several of my modules to the main server file. These modules encapsulate some set of functions. Other own modules are also connected to those modules. And here, for example, an error occurs in the files of the third level. How to catch her? In the file in which it happened? Or will it return to the place where the function was called in the main server file and can be processed by a special middleware?

//server.js
var api = require('first');
api.run() <-- здесь

//first.js
var second = require('second');

module.exports = {
     run: function() { second.solve() }
}

//second.js

module.exports = {
    solve: function(){ return 1/0; }   <-- или здесь
}

And if, of course, callbacks are used, how in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Pushkarev, 2014-11-13
@rock

The file doesn't matter. Synchronous code - try-catch , asynchronous - domains (for example, in middleware) or Promise . Anything not caught is an uncaughtException .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question