Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
request-promise's error object properties (online vs offline output comparison)
{
"error": {
"name": "RequestError",
"message": "Error: getaddrinfo ENOTFOUND somewhere.com somewhere.com:80",
"cause": {
"code": "ENOTFOUND",
"errno": "ENOTFOUND",
"syscall": "getaddrinfo",
"hostname": "somewhere.com",
"host": "somewhere.com",
"port": 80
},
"error": {
"code": "ENOTFOUND",
"errno": "ENOTFOUND",
"syscall": "getaddrinfo",
"hostname": "somewhere.com",
"host": "somewhere.com",
"port": 80
},
"options": {
"url": "http://somewhere.com/some/place",
"qs": {
"someQueryParam": 123456
},
"json": true,
"simple": true,
"resolveWithFullResponse": false,
"transform2xxOnly": false
}
}
}
{
"error": {
"name": "StatusCodeError",
"statusCode": 503,
"message": "503 - \"The service is not available. Please try again later.\"",
"error": "The service is not available. Please try again later.",
"options": {
"url": "http://somewhere.com/some/place",
"qs": {
"someQueryParam": 123456
},
"json": true,
"simple": true,
"resolveWithFullResponse": false,
"transform2xxOnly": false
},
"response": {
"statusCode": 503,
"body": "The service is not available. Please try again later.",
"headers": {
"content-type": "text/html",
"content-length": "53",
"expires": "now",
"pragma": "no-cache",
"cache-control": "no-cache,no-store"
},
"request": {
"uri": {
"protocol": "http:",
"slashes": true,
"auth": null,
"host": "somewhere.com",
"port": null,
"hostname": "somewhere.com",
"hash": null,
"search": "?someQueryParam=123456",
"query": "someQueryParam=123456",
"pathname": "/some/place",
"path": "/some/place?someQueryParam=123456",
"href": "http://somewhere.com/some/place/?someQueryParam=123456"
},
"method": "GET",
"headers": {
"accept": "application/json"
}
}
}
}
}
@nbkhope

This comment has been minimized.

Copy link
Owner Author

commented Jun 22, 2017

Let offline be "off" and online be "on."

Both the off and on case have a name and message property. For the off case, the error property is an object; for the on case, the error property is just a string that contains part of the text in the message property. The off case does not include a response property because there's no reply. The on case provides a response property that is an object that includes many details about it.

In terms of error handling, you might be particularly interested in sending an error response to the client using the message property for both off and on case. You could first check if there's a response property on the error object and then use the statusCode property as the reply code; if there's no response, just send any desired code (for example, 504 Gateway Timeout).

Some Node.js code to illustrate the paragraph above:

if (error.response) {
  res.status(error.statusCode);
}
else {
  res.status(504);
}
res.send({ message: error.message });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.