Notes on Error handling
The hallmark of an experienced developer is his/her ability to foresee the errors and handle them appropriately. Below are some notes regarding how error handling is done in different contexts. Let’s first take a look at the general structure of error handling. Structure of error handling Within a .then().catch() block For instance, within a route, you want to find the noteById, and then do something. 1 2 3 4 5 6 Note.findById(id) .then(note => note) .catch(error => { console.log(error) response.status(500).end() }) Often times, when you’re making a request to the database, it’s unclear whether your request will succeed. If there is an error, you’ll need a .catch() block to handle the error. The catch block receives a callback function with error as argument - an error handler function - and you’ll then define how to handle this error. ...