Facing 'Unhandled error' in NodeJs even I'm after writing process error handler events. Is there any other way to handle these kind of errors? - node.js

I'm working on a node project, but facing difficulty to handle errors.
I kept both
process.on('unhandledRejection', (err, promise) => {
console.log(`Error: ${err.message}`.red);
});
process.on('uncaughtException', (err, origin) => {
console.log(`Error: ${err.message} \n origin: ${origin}`.red);
});
Still facing Unhandled error,...
Anyone have any suggestions?
I want Unhandled errors to be handled.

Related

Throwned Error cannot be handled in createReadStream in Node.js using express-js

I'm trying to handle an exception thrown in an fs.createReadStream, and I use a Promise for that, but the error terminates the API, despite the .catch.
What am I doing wrong ?
Here is my test code :
fcts = require('./functions.js');
app.get('/test', function(req, res){
console.log("OK TEST")
fcts.test(req, res, '/data/file')
.then(result => console.log("OK"))
.catch(err => console.log("NO"))
})
module.exports = {
test: function (request, response, fichier){
return new Promise((resolve, reject) => {
var flux = fs.createReadStream(fichier)
.on('error', function(err){
console.log("no")
reject("reject")
})
.on('close', function(){
console.log("end")
resolve("ok")
})
.pipe(unzip.Parse())
.on('entry', function (entry) {
throw("uh")
});
console.log("end end")
})
}
}
Here are my logs:
...
OK TEST
end end
.../functions.js:1493
throw("uh")
^
uh
I am using node 8.16.0 and express-js
Thanks !
It will work if you reject instead of throwing :
.on('entry', function (entry) {
reject(new Error('uh'));
});
Throwing in a callback used by an async function inside a Promise definition function will not work, because the context in wich it have been thrown is not the promise one, but the one of the createReadStream that don't seems to trigger the error event for an exception thrown in a callback you defined with on.
In fact, in your case, the error that is thrown is an UncaughtException, because the context in wich the error occurs doesn't catch it. Since it is in an asynchronous context, it's the node.js context that notice the error.
Why ? Maybe because the error event in only triggered for stream errors, not for callback executions failures.
When running your script in nodejs, you should use the flag --trace-uncaught nodejs --trace-uncaught ./myfile.js. it will prompt you more infos on uncaught exceptions when it occurs.

Error handled by both express and process.on('uncaughtException) in node.js

I have a problem with an error appearing on one of my express route. I handled it by using express next but then the error is also caught by my global process.on('uncaughtException') that is implemented to shut-down my server. Hope this example is clear.
app.get("/api/users", async (req, res, next) => {
try {
// (1) This call will throw an error
const users = await getUsers()
res.send(users)
}
catch (err) {
// (2) The error will be correctly caught by this try catch and send to the error handler
next(err)
}
})
// (3) The problem is that it will be also caught by this and my server will stop
// I don't want this since I've already handled it using express
process.on('uncaughtException', (err: Error) => cleanShutdown(`Uncaught exception`, err))
Is it possible to avoid going to (3) when I have already handled the error?

How to avoid http2 connect from crashing Node.js

const client = http2.
connect('https://domain.doesnt.exist').
on('error', e => {
console.error(e.message);
});
On Node.js 13.6.0, even when provided an error listener, the above code would crash the entire process.
How will I be able to avoid it?
you can use uncaughtException event to log all exceptions not been caught..
process.on('uncaughtException', err => {
console.log(err.message);
console.log(err.stack);
process.exit(1)
})

How to capture the errors in node.js using uncaughtException

In my application i want to create own module to capture my application error using uncaughtException.If i create uncaughtException in same module means its capturing errors but if i create that uncaughtException in separate module.Then call that module means its not capturing erros.Can anyone help me to fix this issue.
module1.js
var errorModule=require('./module2');
var err = new Error('Something went terribly wrong');
errorModule.captureError(err);
module2.js
module.exports.captureError=function(err){
process.on('uncaughtException', function(err) {
console.log(err);
});
}
Try this:
// module1.js
var errorModule=require('./module2');
errorModule.captureErrors();
throw Error('Something went terribly wrong');
// module2.js
module.exports.captureErrors = function() {
process.on('uncaughtException', function(err) {
console.log('an error occurred', err);
});
};
A few things to notice:
process.on('uncaughtException', ...) installs an event handler to catch uncaught exceptions; your code tries to pass an error to it, but that seems to defeat what you're writing ('to capture my application error using uncaughtException');
Uncaught exceptions are errors which are thrown (throw Error(...));
If you want the code in your module1 to work, module2 needs to look like this:
module.exports.captureError = function(err) {
console.log(err);
};
But that has nothing to do with uncaughtException.

Node.js doesn't display entire error message on uncaughtException, is it possible?

In node.js if you catch uncaughtExceptions, like so:
process.on('uncaughtException', function (error) {
console.log(error);
});
The error message displayed doesn't contain all the information that you receive if you don't catch the error and just let the process crash. When you let the process crash it includes what line caused the error. Is there any way to get the full error message including the line that caused the error so we can log this data using uncaughtException.
Try error.stack
process.on('uncaughtException', function (error) {
console.log(error.stack);
});
Try:
process.on('uncaughtException', function (error) {
console.dir(error);
});

Resources