|
/** |
|
* Map HTTP error codes to fetch's .then/.catch behavior |
|
* List based on https://developer.mozilla.org/en-US/docs/Web/HTTP/Status |
|
*/ |
|
|
|
let codes = [ |
|
{"code": "100", "text": "Continue"}, |
|
{"code": "101", "text": "Switching Protocol"}, |
|
{"code": "200", "text": "OK"}, |
|
{"code": "201", "text": "Created"}, |
|
{"code": "202", "text": "Accepted"}, |
|
{"code": "203", "text": "Non-Authoritative Information"}, |
|
{"code": "204", "text": "No Content"}, |
|
{"code": "205", "text": "Reset Content"}, |
|
{"code": "206", "text": "Partial Content"}, |
|
{"code": "300", "text": "Multiple Choice"}, |
|
{"code": "301", "text": "Moved Permanently"}, |
|
{"code": "302", "text": "Found"}, |
|
{"code": "303", "text": "See Other"}, |
|
{"code": "304", "text": "Not Modified"}, |
|
{"code": "305", "text": "Use Proxy"}, |
|
{"code": "306", "text": "unused"}, |
|
{"code": "307", "text": "Temporary Redirect"}, |
|
{"code": "308", "text": "Permanent Redirect"}, |
|
{"code": "400", "text": "Bad Request"}, |
|
{"code": "401", "text": "Unauthorized"}, |
|
{"code": "402", "text": "Payment Required"}, |
|
{"code": "403", "text": "Forbidden"}, |
|
{"code": "404", "text": "Not Found"}, |
|
{"code": "405", "text": "Method Not Allowed"}, |
|
{"code": "406", "text": "Not Acceptable"}, |
|
{"code": "407", "text": "Proxy Authentication Required"}, |
|
{"code": "408", "text": "Request Timeout"}, |
|
{"code": "409", "text": "Conflict"}, |
|
{"code": "410", "text": "Gone"}, |
|
{"code": "411", "text": "Length Required"}, |
|
{"code": "412", "text": "Precondition Failed"}, |
|
{"code": "413", "text": "Payload Too Large"}, |
|
{"code": "414", "text": "URI Too Long"}, |
|
{"code": "415", "text": "Unsupported Media Type"}, |
|
{"code": "416", "text": "Requested Range Not Satisfiable"}, |
|
{"code": "417", "text": "Expectation Failed"}, |
|
{"code": "421", "text": "Misdirected Request"}, |
|
{"code": "426", "text": "Upgrade Required"}, |
|
{"code": "428", "text": "Precondition Required"}, |
|
{"code": "429", "text": "Too Many Requests"}, |
|
{"code": "431", "text": "Request Header Fields Too Large"}, |
|
{"code": "451", "text": "Unavailable For Legal Reasons"}, |
|
{"code": "500", "text": "Internal Server Error"}, |
|
{"code": "501", "text": "Not Implemented"}, |
|
{"code": "502", "text": "Bad Gateway"}, |
|
{"code": "503", "text": "Service Unavailable"}, |
|
{"code": "504", "text": "Gateway Timeout"}, |
|
{"code": "505", "text": "HTTP Version Not Supported"}, |
|
{"code": "506", "text": "Variant Also Negotiates"}, |
|
{"code": "507", "text": "Variant Also Negotiates"}, |
|
{"code": "511", "text": "Network Authentication Required"} |
|
]; |
|
|
|
codes.map(({ code , text }) => { |
|
let start = new Date(); |
|
fetch(`http://localhost:8080/?code=${code}`) |
|
.then((response) => { |
|
console.log(`${code} -> resolve`); |
|
}) |
|
.catch((reason) => { |
|
let end = new Date(); |
|
let elapsed = end.getTime() - start.getTime(); |
|
console.log(`${code} -> reject reason=${reason} (${elapsed}ms)`); |
|
}); |
|
}); |