From 52698489480e4803960ce29765979e687ff81ff0 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Wed, 7 Apr 2021 23:02:49 +0200 Subject: site: better error messages this includes deduplicating some code, and attempts at error messages for files which could not be parsed as json but aren't actually valid age-encrypted files (there doesn't seem to be some easy way to validate that these files are valid age files without knowing the key). --- site/index.html | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/site/index.html b/site/index.html index 6dc12fa..ced3af8 100644 --- a/site/index.html +++ b/site/index.html @@ -169,7 +169,7 @@ console.log(response.text()) window.location.href = "thanks.html"; } - errormsg.innerText = "POST request returned error code: " + errormsg.innerText = "POST request returned error code:\n" + response.status + ": " + response.statusText; errormsg.hidden = false; }).catch(error => { @@ -225,11 +225,22 @@ }; } + function mkReadError (msg) { + appendChildren(root, [ + mkElement("h1", "Error"), + mkElement("p", msg), + appendChildren( + mkElement("p","attempted path: "), + [mkElement("tt", surveyUrl)] + ) + ]); + } + async function main () { // initialise the web assembly parts of this await init(); const Http = new XMLHttpRequest(); - const url= surveyUrl; + const url = surveyUrl; Http.open("GET", url); Http.responseType = "arraybuffer"; Http.send(); @@ -243,31 +254,33 @@ try { survey = JSON.parse(string); } catch (e) { - console.log ("survey appears to be encrypted"); - askPassphrase(bytearray); + if (string.lastIndexOf("age-encryption.org/v1") === 0) { + console.log ("survey appears to be encrypted"); + askPassphrase(bytearray); + } else { + mkReadError ("Could not load this survey; it appears to be in a wrong or unknown format."); + } } /// if the survey was unencrypted, start it here. If it /// was encrypted, we need to wait for user action via - /// a js callback (handled in askPassphrase). + /// a js callback (handled in askPassphrase). This is + /// out here only to avoid catching errors from within mkSurvey. if (survey !== null) { mkSurvey(survey); } // couldn't load survey json, show error message } else { - appendChildren(root, [ - mkElement("h1", "Error"), - mkElement("p", "Could not load this survey; are you sure that it exists?"), - appendChildren( - mkElement("p","attempted path: "), - [mkElement("tt", surveyUrl)] - ) - ]); + mkReadError ("Could not load this survey; are you sure that it exists?"); } } } } + if (surveyUrl === "") { + mkReadError ("There's nothing here. Maybe try appending something after the url?\n\nAlternatively, check to see if you copied the whole url."); + } else { + main () + } - main () -- cgit v1.2.3