|
| 1 | +const CORS_ERROR_URL = "https://www.example.com"; |
| 2 | + |
| 3 | +async function corsUncaughtFetch() { |
| 4 | + return await fetch(CORS_ERROR_URL); |
| 5 | +} |
| 6 | + |
| 7 | +async function corsUncaughtFetchSameOrigin() { |
| 8 | + return await fetch(CORS_ERROR_URL, { |
| 9 | + mode: "same-origin" |
| 10 | + }); |
| 11 | +} |
| 12 | + |
| 13 | +async function corsCaughtFetch() { |
| 14 | + try { |
| 15 | + await corsUncaughtFetch(); |
| 16 | + } catch (e) { |
| 17 | + return console.log("Caught error from fetch", e); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +function corsUncaughtXHRSync() { |
| 22 | + const xhr = new XMLHttpRequest(); |
| 23 | + xhr.open("GET", CORS_ERROR_URL, false); |
| 24 | + xhr.send(); |
| 25 | +} |
| 26 | + |
| 27 | +function corsCaughtXHRSync() { |
| 28 | + try { |
| 29 | + corsUncaughtXHRSync(); |
| 30 | + } catch (e) { |
| 31 | + console.log("Caught error from synchronous XHR", e); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function corsXHRAsyncWithoutError() { |
| 36 | + const xhr = new XMLHttpRequest(); |
| 37 | + xhr.open("GET", CORS_ERROR_URL); |
| 38 | + xhr.send(); |
| 39 | + return xhr; |
| 40 | +} |
| 41 | + |
| 42 | +function corsXHRAsyncWithError() { |
| 43 | + const xhr = corsXHRAsyncWithoutError(); |
| 44 | + xhr.onerror = function onerror(event) { |
| 45 | + console.log("Error handler triggered from asynchronous XHR", event); |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +const HTTP_404_URL = `${document.location.origin}/this-document-does-not-exist`; |
| 50 | + |
| 51 | +async function http404FetchWithoutOkOrStatusOrStatusTextRead() { |
| 52 | + const response = await fetch(HTTP_404_URL); |
| 53 | + return response; |
| 54 | +} |
| 55 | + |
| 56 | +async function http404FetchWithOkRead() { |
| 57 | + const response = await http404FetchWithoutOkOrStatusOrStatusTextRead(); |
| 58 | + if (response.ok) { |
| 59 | + console.log("fetch response was ok"); |
| 60 | + } else { |
| 61 | + console.log("fetch response was not ok"); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +async function http404FetchWithStatusRead() { |
| 66 | + const response = await http404FetchWithoutOkOrStatusOrStatusTextRead(); |
| 67 | + console.log("fetch response status is", response.status); |
| 68 | +} |
| 69 | + |
| 70 | +async function http404FetchWithStatusTextRead() { |
| 71 | + const response = await http404FetchWithoutOkOrStatusOrStatusTextRead(); |
| 72 | + console.log("fetch response statusText is", response.statusText); |
| 73 | +} |
0 commit comments