Thursday, 28 December 2017

Sending an error to Request

Hey everyone, I have a question about http error objects, specifically when using node and the Request module.What kind of object do I send to the Request callback function so that it shows up as the error object?My Request call looks like this:request({ url: url, method: 'GET' }, function(error, response, body) { // HOW DO I GET an 'error' HERE???? } That is pretty straightforward and it works fine with the api I wrote, which looks like this:User.findOne({_id: req.params.id}) .then(user => res.status(200).send(user)) .catch(err => res.status(500).send(err)); The problem is that the Request call above never receives an 'error' object no matter what I do. When the last line of the api sends back 'err', it is received by Request as the 'body', not the 'error' object.I have messed around with that line a bunch, like creating a new Error object and sending that back, or changing the status code, or sending a custom object. Nothing seems to ever show up in that error object in the Request callback function.The Request docs say that an error would come from a http.ClientRequest object, but I couldn't make much sense out of that.Any guidance here is appreciated. Thanks!

Submitted December 29, 2017 at 06:21AM by ithurtsbutitsworthit

No comments:

Post a Comment