PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 14, 2022

[FIXED] How to handle all exceptions in node.js

 November 14, 2022     error-handling, node.js     No comments   

Issue

After working for a couple of weeks with node.js, I found that there is a difference between node.js server errors and regular server side languages like PHP.

A simple example: IF an error happens in our website for ANY reason.

in PHP
If a user send some invalid data to server and MySQL, MySQL will output error to that specific user and the whole application won't go down.

in Nodejs
If a user send some invalid data to server and MySQL, nodejs Server will go down and so all the users will disconnect and there is no connection between users anymore.

This is a really big problem. in large web applications, It is impossible to handle all errors to avoid Nodejs server to go down, and the question is,
Is there any way to handle any unknown fatal errors and exceptions to a specific output or something like it.


Solution

You can use the uncaughtException event on the process object to do what you want, but like others have said, domains and catching/handling errors at the correct level is recommended.

process.on('uncaughtException', function(err) {
  console.log('Caught exception: ' + err);
});


Answered By - BadCanyon
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing