How To Deal With Returned Object On Error In A Promise-based Business-level Function In Node.js?
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:
- Register the error
- Determine the error type
- Operational errors, such as failing to connect to a server
- Programmer error, which are bugs and needs to be fixed by a developer
Operational errors can be ignored by
- Unit Testing
- Taking care of asynchronous and synchronous calls
- Ensuring all arguments are not passed to the function
- 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?"