Browse Source

fix: TypeError: Invalid character in Set-Cookie header content

Sv443 1 week ago
parent
commit
0c615e38f2
2 changed files with 18 additions and 14 deletions
  1. 17 13
      docs/raw/errorPage.js
  2. 1 1
      src/httpServer.js

+ 17 - 13
docs/raw/errorPage.js

@@ -1,18 +1,18 @@
-window.errorWrittenToPage = false;
+let errorWrittenToPage = false;
 
 document.addEventListener("DOMContentLoaded", function() {
     setTimeout(function() {
-        if(window.errorWrittenToPage != true)
-        {
+        if(errorWrittenToPage != true)
             setErrorDisp(500, "Internal Server Error", "Error while finding the error message - oh the irony");
-        }
-    }, 6000);
+    }, 5000);
 
     try
     {
-        let errorInfo = JSON.parse(Cookies.get("errorInfo")); // eslint-disable-line no-undef
+        let errorInfo = JSON.parse(atob(Cookies.get("errorInfo"))); // eslint-disable-line no-undef
 
-        let statusCode = parseInt(errorInfo["API-Error-StatusCode"]);
+        let statusCode = Number(errorInfo ? errorInfo.code : NaN);
+        if(isNaN(statusCode))
+            statusCode = 500;
         let errorReasonMsg = "";
         let errorSubtext = "";
 
@@ -22,13 +22,17 @@ document.addEventListener("DOMContentLoaded", function() {
                 errorReasonMsg = "Not Found";
                 errorSubtext = "<!--%#INSERT:NAME#%--> couldn't find a resource that corresponds to the URL you have entered.<br>Please make sure the URL is correct or <a href=\"<!--%#INSERT:DOCSURL#%-->\">visit the documentation by clicking here</a>.";
             break;
+            case 429:
+                errorReasonMsg = "Too Many Requests";
+                errorSubtext = "You have sent too many requests to the server in a short amount of time.<br>Please wait a few minutes and try again.";
+            break;
             case 500: default:
                 errorReasonMsg = "Internal Server Error";
                 errorSubtext = "<!--%#INSERT:NAME#%--> encountered an unexpected internal error.<br>If this error persists and error details were provided on this page, please <a href=\"<!--%#INSERT:AUTHORWEBSITEURL#%-->\">contact me</a> with the error details and I will try to fix it and/or help you.<br>Alternatively, <a href=\"<!--%#INSERT:DOCSURL#%-->\">visit the documentation by clicking here</a>.";
             break;
         }
 
-        setErrorDisp(parseInt(statusCode), errorReasonMsg, errorInfo["API-Error-Message"], errorSubtext);
+        setErrorDisp(statusCode, errorReasonMsg, errorInfo.message, errorSubtext);
     }
     catch(err)
     {
@@ -45,11 +49,11 @@ document.addEventListener("DOMContentLoaded", function() {
  */
 function setErrorDisp(code = 500, summary = "Internal Server Error", details = "No details provided", subText = "")
 {
-    window.errorWrittenToPage = true;
+    errorWrittenToPage = true;
     document.title = ("<!--%#INSERT:NAME#%--> - Error " + code.toString());
-    document.getElementById("errCodeDisplay").innerHTML = (code.toString() + " - " + summary);
-    document.getElementById("errDetailDisplay").innerHTML = "Details: " + details;
+    document.getElementById("errCodeDisplay").innerText = (code.toString() + " - " + summary);
+    document.getElementById("errDetailDisplay").innerText = "Details: " + details;
 
     if(subText)
-        document.getElementById("errSubText").innerHTML = subText;
-}
+        document.getElementById("errSubText").innerText = subText;
+}

+ 1 - 1
src/httpServer.js

@@ -586,7 +586,7 @@ const respondWithErrorPage = (res, statusCode, error) => {
 
     if(!jsl.isEmpty(error))
     {
-        res.setHeader("Set-Cookie", `errorInfo=${JSON.stringify({"API-Error-Message": error, "API-Error-StatusCode": statusCode})}`);
+        res.setHeader("Set-Cookie", `errorInfo=${btoa(JSON.stringify({ "message": error, "code": statusCode }))}`);
         res.setHeader("API-Error", error);
     }