feat(extension): better error handling
Some checks failed
Publish docker image / Publish (push) Has been cancelled
Some checks failed
Publish docker image / Publish (push) Has been cancelled
This commit is contained in:
parent
2425cab043
commit
fd63bbb091
3 changed files with 39 additions and 6 deletions
|
@ -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;
|
||||||
|
}
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -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));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Reference in a new issue