Add non-object error handling for API response - #9
Conversation
Updating Github Actions & Adding config.json
… Machine Readability Creating Release with Dereferenced Symlinks, `polarityIntegrationUuid`, & No Dev Dependencies for Machine Readability
… Machine Readability Removing `-beta` from `version` field in `package.json` & `package-lock.json`
Removing rejectUnauthorized from config files
INT-1107: Converted expandable types & Removed `requestOptions` from `config.json`
INT-1426: Improve error handling, update dependencies, remove use of summary component
This commit addresses a `TypeError: Cannot read properties of undefined (reading 'error')` that can occur when the API returns an unexpected status code without a JSON body. The previous handleRestError function did not properly validate the body object, causing the application to error.
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
| error: body ? body : 'Unknown Error', | ||
| detail: | ||
| body && body.error && body.message | ||
| body && typeof body === 'object' && body.error && body.message |
There was a problem hiding this comment.
@eljeffeg Thank you for the PR! Just curious on this but were you seeing a case where the existing guard was failing? The specific error message you referenced suggests body was undefined which should be handled by the check that is currently in place.
TypeError: Cannot read properties of undefined (reading 'error')
We're actually forcing the request library we're using to parse the body as json (via the json: true attribute on the request options), so if body wasn't valid JSON I think we would actually get a different error entirely.
Finally, can you update the PR to target the develop branch?
There was a problem hiding this comment.
Yeah, I got the error while trying to run it on https://polarityscribe.polarity.io/. I then loaded it up locally and got the error Unexpected HTTP Status Code 500 received.. Maybe I'm doing something wrong, but I verified it using curl curl -X GET 'https://api.threatminer.org/v2/sample.php?q=dd2b6e3aa75de8460730862f2dc739537734a7dfc9e673b6a23ee58430348ddf&rt=1' -v -i. ThreatMiner is returning a 500. I tried a few different hashes, even going directly to the site https://api.threatminer.org/v2/sample.php?q=dd2b6e3aa75de8460730862f2dc739537734a7dfc9e673b6a23ee58430348ddf&rt=1
Looks like a problem with ThreatMiner. So I don't think there is anything wrong with the integration from what I can tell unless ThreatMiner changed their API; we just weren't handling the case that an error would return without a proper body.
This commit addresses a
TypeError: Cannot read properties of undefined (reading 'error')that can occur when the API returns an unexpected status code without a JSON body. The previous handleRestError function did not properly validate the body object, causing the application to error.