Skip to content

Commit

Permalink
[main] retrieve reponseText when fetch in HttpManager (#2185)
Browse files Browse the repository at this point in the history
  • Loading branch information
siyuniu-ms authored Oct 27, 2023
1 parent e535c94 commit 0398605
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions channels/1ds-post-js/src/HttpManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,23 @@ export class HttpManager {
requestInit.headers = payload.headers;
}

const handleResponse = (status: number, headerMap: { [x: string]: string; }, responseText: string) => {
if (!responseHandled) {
responseHandled = true;
_doOnComplete(oncomplete, status, headerMap, responseText);
_handleCollectorResponse(responseText);
}
};

const handleError = () => {
// In case there is an error in the request. Set the status to 0
// so that the events can be retried later.
if (!responseHandled) {
responseHandled = true;
_doOnComplete(oncomplete, 0, {});
}
};

fetch(theUrl, requestInit).then((response) => {
let headerMap = {};
let responseText = STR_EMPTY;
Expand All @@ -482,22 +499,12 @@ export class HttpManager {
if (response.body) {
response.text().then(function(text) {
responseText = text;
});
}

if (!responseHandled) {
responseHandled = true;
_doOnComplete(oncomplete, response.status, headerMap, responseText);
_handleCollectorResponse(responseText);
}
}).catch((error) => {
// In case there is an error in the request. Set the status to 0
// so that the events can be retried later.
if (!responseHandled) {
responseHandled = true;
_doOnComplete(oncomplete, 0, {});
handleResponse(response.status, headerMap, responseText);
}, handleError);
} else {
handleResponse(response.status, headerMap, "");
}
});
}).catch(handleError);

if (ignoreResponse && !responseHandled) {
// Assume success during unload processing
Expand Down

0 comments on commit 0398605

Please sign in to comment.