Improve error message handling

This commit is contained in:
nicolas.dorier 2024-02-29 09:27:01 +09:00
parent d5c8283d01
commit 27150bcd4e
No known key found for this signature in database
GPG key ID: 6618763EF09186FE

View file

@ -54,8 +54,14 @@
var abortController = null; var abortController = null;
function handleError(e){ function handleError(e){
document.querySelector("#error p").innerHTML = e.message; if (e) {
document.getElementById("error").classList.remove("d-none"); document.querySelector("#error p").innerHTML = e.message;
document.getElementById("error").classList.remove("d-none");
}
else
{
document.getElementById("error").classList.add("d-none");
}
} }
function delay(ms) { function delay(ms) {
@ -64,13 +70,13 @@
async function showBalance(lnurlw) { async function showBalance(lnurlw) {
try { try {
setState("Submitting"); setState("Submitting");
await delay(1000); var uiDelay = delay(1000);
var url = window.location.href.replace("#", ""); var url = window.location.href.replace("#", "");
url = url.split("?")[0] + "?" + lnurlw.split("?")[1]; url = url.split("?")[0] + "?" + lnurlw.split("?")[1];
// url = "https://testnet.demo.btcpayserver.org/boltcards/balance?p=...&c=..." // url = "https://testnet.demo.btcpayserver.org/boltcards/balance?p=...&c=..."
var xhttp = new XMLHttpRequest(); var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () { xhttp.onreadystatechange = async function () {
if (this.readyState == 4 && this.status == 200 && this.responseText) { if (this.readyState == 4 && this.status == 200 && this.responseText) {
document.getElementById("balance-table").innerHTML = this.responseText; document.getElementById("balance-table").innerHTML = this.responseText;
document.getElementById("CancelWizard").addEventListener("click", function (e) { document.getElementById("CancelWizard").addEventListener("click", function (e) {
@ -88,12 +94,12 @@
el[i].classList.add("d-none"); el[i].classList.add("d-none");
} }
}); });
await uiDelay;
setState("ShowBalance"); setState("ShowBalance");
} }
else if(this.readyState == 4 && this.status == 404) { else if(this.readyState == 4 && this.status == 404) {
setState("WaitingForCard"); setState("WaitingForCard");
handleError(new Error("Card not initialized")); handleError(new Error("This card is initialized, but not by us"));
} }
else { else {
setState("WaitingForCard"); setState("WaitingForCard");
@ -118,7 +124,13 @@
await ndef.scan({ signal: abortController.signal }) await ndef.scan({ signal: abortController.signal })
setState("WaitingForCard"); setState("WaitingForCard");
ndef.onreading = async ({ message }) => { ndef.onreading = async ({ message }) => {
handleError(null);
const record = message.records[0]; const record = message.records[0];
if (message.records.length === 0)
{
handleError(new Error("This card hasn't been initialized"));
return;
}
const textDecoder = new TextDecoder('utf-8'); const textDecoder = new TextDecoder('utf-8');
const decoded = textDecoder.decode(record.data); const decoded = textDecoder.decode(record.data);
await showBalance(decoded); await showBalance(decoded);