Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty payload handling in pxhttpc #293

Merged
merged 5 commits into from
Dec 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions lib/pxhttpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ function callServer(data, headers, uri, callType, config, callback) {
if (err.toString().toLowerCase().includes('timeout')) {
return callback('timeout');
} else {
return handleError(callback, S2SErrorReason.UNKNOWN_ERROR, `encountered error with risk api call: ${err}`, response);
return handleError(
callback,
S2SErrorReason.UNKNOWN_ERROR,
`encountered error with risk api call: ${err}`,
response,
);
}
}

Expand All @@ -46,7 +51,7 @@ function callServer(data, headers, uri, callType, config, callback) {
callback,
S2SErrorReason.UNKNOWN_ERROR,
`call to perimeterx server returned null or empty response: ${response}`,
response
response,
);
}

Expand All @@ -65,20 +70,35 @@ function callServer(data, headers, uri, callType, config, callback) {
const handleOkResponse = (callback, response) => {
let data = getDataFromResponse(response);

if (!data) {
return handleError(callback, S2SErrorReason.INVALID_RESPONSE, `unable to get data from response body: ${response.body}`, response);
if (!data && response.statusCode >= 300) {
pxjohnny marked this conversation as resolved.
Show resolved Hide resolved
return handleError(
callback,
S2SErrorReason.INVALID_RESPONSE,
`unable to get data from response body: ${response.body}`,
response,
);
}

if (typeof data !== 'object') {
try {
data = JSON.parse(data);
} catch (e) {
return handleError(callback, S2SErrorReason.INVALID_RESPONSE, `error parsing response body - ${data}: ${e}`, response);
return handleError(
callback,
S2SErrorReason.INVALID_RESPONSE,
`error parsing response body - ${data}: ${e}`,
response,
);
}
}

if (data.status && data.status !== 0) {
return handleError((err) => callback(err, data), S2SErrorReason.REQUEST_FAILED_ON_SERVER, data.message, response);
if (data && data.status && data.status !== 0) {
return handleError(
(err) => callback(err, data),
S2SErrorReason.REQUEST_FAILED_ON_SERVER,
data.message,
response,
);
}

return callback(null, data);
Expand Down
Loading