Skip to content

Commit

Permalink
Feat: Improve the response handling flow.
Browse files Browse the repository at this point in the history
- This gives the body being passed to the handlers more meaning.
  • Loading branch information
Codex- committed Nov 26, 2021
1 parent 7aa0eec commit e88a5e1
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/raygun.network-tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,11 @@ window.raygunNetworkTrackingFactory = function(window, Raygun) {

self.executeHandlers(self.requestHandlers, metadata);

promise.then(
self.wrapWithHandler(function(response) {
var body = 'N/A when the fetch response does not support clone()';
promise
.then(function (response) {
var ourResponse = typeof response.clone === 'function' ? response.clone() : undefined;

function executeHandlers() {
function executeResponseHandlers(body) {
Raygun.Utilities.log('tracking fetch response for', url);
self.executeHandlers(self.responseHandlers, {
status: response.status,
Expand All @@ -205,30 +204,37 @@ window.raygunNetworkTrackingFactory = function(window, Raygun) {
}

if (ourResponse && typeof ourResponse.text === 'function') {
// Return the promise so that it may be handled by the
// parent catch should `executeHandlers` fail.
return ourResponse.text()
.then(function(text) {
body = Raygun.Utilities.truncate(text, 500);

executeHandlers();
})
.catch(function() { executeHandlers(); });
// Return the promise so that it may be handled by the parent catch.
return ourResponse.text().then(
// Fulfilled
function (text) {
var truncatedResponseText = Raygun.Utilities.truncate(text, 500);
executeResponseHandlers(truncatedResponseText);
},
// Rejected
function (reason) {
var err = '';
if (reason instanceof Error) {
err = ': ' + reason.message;
} else if (typeof reason === 'string') {
err = ': ' + reason;
}
executeResponseHandlers('N/A response.text() rejected' + err);
}
);
}

executeHandlers();
executeResponseHandlers('N/A when the fetch response does not support clone()');
})
).catch(
function(error) {
.catch(function (error) {
self.executeHandlers(self.errorHandlers, {
metadata: {
requestUrl: url,
error: error.toString(),
duration: new Date().getTime() - initTime,
},
});
}
);
});

return promise;
};
Expand Down

0 comments on commit e88a5e1

Please sign in to comment.