feat(extension): better error handling
Some checks failed
Publish docker image / Publish (push) Has been cancelled

This commit is contained in:
Ashhhleyyy 2024-02-19 18:16:40 +00:00
parent 2425cab043
commit fd63bbb091
Signed by: ash
GPG key ID: 83B789081A0878FB
3 changed files with 39 additions and 6 deletions

View file

@ -239,3 +239,16 @@ tr.code-bad {
td.code-bad { td.code-bad {
text-decoration: line-through; text-decoration: line-through;
} }
.error {
display: flex;
align-items: center;
gap: .5em;
}
.error::before {
content: "! ";
color: red;
font-weight: 900;
font-size: 2em;
}

View file

@ -56,7 +56,7 @@ function interceptSubmit(details) {
code: activity.code, code: activity.code,
success: codeCorrect, success: codeCorrect,
}), }),
}).then(res => console.log('submitted code: ', res)).error(e => console.error('failed to submit code', e)); }).then(res => console.log('submitted code: ', res)).catch(e => console.error('failed to submit code', e));
} }
function interceptCode(details) { function interceptCode(details) {

View file

@ -44,14 +44,34 @@
return root; return root;
} }
const res = await fetch(SERVER_URL + '/api/codes'); function message(message, error = false) {
const ele = document.createElement('p');
if (error) {
ele.classList.add('error');
const s = document.createElement('span');
s.innerText = message;
ele.appendChild(s);
} else {
ele.innerText = message;
}
root.appendChild(ele);
}
const res = await fetch(SERVER_URL + '/api/codes').catch((e) => {
message(`Something went wrong: ` + e);
console.error(e);
});
if (!res.ok) { if (!res.ok) {
// TODO: Handle error message('Server returned error code ' + res.status + ': ' + res.statusText, true);
return; return;
} }
const { activities } = await res.json(); const { activities } = await res.json();
if (activities.length === 0) {
message('No codes are available right now :/');
} else {
activities.sort(activity => activity.time); activities.sort(activity => activity.time);
for (const activity of activities) { for (const activity of activities) {
root.appendChild(activityEle(activity)); root.appendChild(activityEle(activity));
} }
}
})(); })();