Skip to content

Commit

Permalink
Add response status handling and new events.
Browse files Browse the repository at this point in the history
  • Loading branch information
hmvictor committed Jul 20, 2020
1 parent 2db6f74 commit 5711c50
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion fetchEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ window.fetch = function(url, init) {
response: response
}
}));
} if(response.status >= 400 && response.status < 600) {
} else if(response.status >= 400 && response.status < 600) {
window.dispatchEvent(
new CustomEvent("fetchResponseError", {
detail: {
Expand Down
12 changes: 8 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,16 @@ <h4>
}
});
window.addEventListener("fetchResponseSuccess", event => {
event.detail.response.json().then(content => {
document.getElementById(window.logId).innerHTML+="<br/>Fetch response success. Url: ["+event.detail.url+"]. HTTP Method: ["+event.detail.init.method+"]. Response: "+content+".";
});
if(window.logId) {
event.detail.response.json().then(jsonResponse => {
document.getElementById(window.logId).innerHTML+="<br/>Fetch response success. Url: ["+event.detail.url+"]. HTTP Method: ["+event.detail.init.method+"]. Response: "+jsonResponse+".";
});
}
});
window.addEventListener("fetchResponseError", event => {
document.getElementById(window.logId).innerHTML+="<br/>Fetch response error. Url: ["+event.detail.url+"]. HTTP Method: ["+event.detail.init.method+"]. Status: ["+event.detail.response.status+"]";
if(window.logId) {
document.getElementById(window.logId).innerHTML+="<br/>Fetch response error. Url: ["+event.detail.url+"]. HTTP Method: ["+event.detail.init.method+"]. Status: ["+event.detail.response.status+"]";
}
});
window.addEventListener("fetchError", event => {
if(window.logId) {
Expand Down

0 comments on commit 5711c50

Please sign in to comment.