Answer the question
In order to leave comments, you need to log in
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; } <-- или здесь
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question