Skip to content Skip to sidebar Skip to footer

How To Deal With Returned Object On Error In A Promise-based Business-level Function In Node.js?

I need to create a business level function called 'getLocationById' which retrieves some data from a remote server via REST API. This function is then called by a router to display

Solution 1:

Your new edits are close! First let's clear up a misconception you have about fetch. Non-OK responses do not result in the fetch promise being rejected. In order to determine if a call has a successful response, check response.ok.

Next we need to investigate the json method. Looking at the documentation we see that it also returns a promise and not JSON.

Here's a version of your router that's a closer to what you're looking for:

router.get('/', function(req, res, next) {
   db_location.getLocations()
   .then(r => {
        if (r.ok) { return r.json(); }
        throw'Something went wrong!';
    })
   .then(data => res.json(data))
   .catch((err) => {
        console.log(err);
        next(err);
    })  
});

I think it's great that you're learning promises. Once you feel comfortable with promises check out async/await. It'll make your code easier to read but having an understanding of promises is important.

Solution 2:

On failure or error, the system needs to do the following:

  1. Register the error
  2. Determine the error type
  3. Operational errors, such as failing to connect to a server
  4. Programmer error, which are bugs and needs to be fixed by a developer

Operational errors can be ignored by

  1. Unit Testing
  2. Taking care of asynchronous and synchronous calls
  3. Ensuring all arguments are not passed to the function
  4. Checking for empty arguments, if false, show an error asking the user to provide required fields

In case of server no connecting, the system should retry after an interval of 60 seconds

Programming errors:

  • Terminate the function with ‘throw ’
  • Record the error
  • Show the user with an appropriate message, without presenting any technical jargony

Solution 3:

If your error needs to be handled by the router you either need to not catch() in getLocationById or catch() there, process what you need, and then return Promise.reject(error) so the router can still take care of it downstream.

Something like:

functiongetLocation() {
  returnPromise.reject('some error') // fake fetch error
  .then(r => r )
  .catch(error => {
    console.log("handling error in getlocation") // maybe logging or other functionsreturnPromise.reject(error)
  })
}

// in routes:getLocation()
.then(r =>console.log("sending to user"))
.catch(error =>console.log("sending error to user: ", error)) 

If you don't have process the error in getLocation() just don't catch it and the error will go down the chain:

functiongetLocation() {
  returnPromise.reject('some error')
  .then(r => r )
}

// in routes:getLocation()
.then(r =>console.log("sending to user"))
.catch(error =>console.log("sending error to user: ", error))

Solution 4:

Try switching to the following code and let me know if it works:

router.get('/', function(req, res, next) {
   return db_location.getLocations().then(r => {
       console.log("r.json(): " + r.json());
       return res.json(r);
     })
     .catch(err => {
       console.log(err);
       returnnext(err);
   })  
});

Post a Comment for "How To Deal With Returned Object On Error In A Promise-based Business-level Function In Node.js?"