Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

node.js - How to show the error alerts on UI whenever the catch error or console error comes and the server stops in localhost or in production side (heroku)?

I want a solution for how I can write the code in nodejs so that whenever console error occurs I want to send the alert erros on UI sid to the user so that he can go back or refresh or login again instead of blocking the page/ website in between whenever the application error logs comes in heroku.

Please provide me a solution for this!!!

Thanks!

Edit:

I am asking for general only. Means just like I was testing my web app on heroku after making live and in between while testing an error occured and I got redirected to the heroku application error log page like this below. So I just want to ignore this and instead of this an alert should appear telling the user to go back or login again as some error occured. but not to break the page in between like this. :( enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The server can't send the response to the client if it crashes completely. When a server crashes it means that your code is not handling the error properly.

As you didn't specify which programming language or framework you are using. I assume that it is Node.js as you mentioned .catch()

In this case, you should have a try/catch block in your code or a .catch if you are using promises. The error occurred but the server won't just crash completly.

You will need to have something similar as below in your route handlers:

Async/Await:

try{
 // Do stuff
}catch(err){ // Bad things happen
 // Log the error so you know what went wrong
 console.log(err)
 // Send the error response to the frontend
 res.status(500).json({msg:'Server Error'})
 
}

Promises:


something
     .then(//Do stuff)
     .catch(err => {  // Bad things happened 
      console.log(err)
      // Send the error response to the frontend
      res.status(500).json({msg:'Server Error'})
      })


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...