diff --git a/fetchEvents.js b/fetchEvents.js index 47e514a..c0bf5d0 100644 --- a/fetchEvents.js +++ b/fetchEvents.js @@ -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: { diff --git a/index.html b/index.html index 22888e4..12a40af 100644 --- a/index.html +++ b/index.html @@ -112,12 +112,16 @@

} }); window.addEventListener("fetchResponseSuccess", event => { - event.detail.response.json().then(content => { - document.getElementById(window.logId).innerHTML+="
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+="
Fetch response success. Url: ["+event.detail.url+"]. HTTP Method: ["+event.detail.init.method+"]. Response: "+jsonResponse+"."; + }); + } }); window.addEventListener("fetchResponseError", event => { - document.getElementById(window.logId).innerHTML+="
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+="
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) {