Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve the output of cards' error info #7

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions html2notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,28 @@ const DEFAULT_DECK_NAME = "IELTS-CamDict-Words";
async function addNotes(notes) {
try {
if (notes == null) throw new Error("addNotes() got a null input of notes");
const wordsListForIndexing = notes.map((v) => v.fields.Word);
// console.log(`wordsListForIndexing: `, wordsListForIndexing);
const result = await invoke("addNotes", 6, { notes: notes });
// console.log(`addNotes() result: `, result);
if (result === null) {

const multiParamsActions = notes.map((note) => ({
action: "addNote",
version: 6,
params: { note },
}));
const results = await invoke("multi", 6, { actions: multiParamsActions });

if (results === null) {
throw new Error("addNotes() returned null");
}
if (result.length == 0) {
if (results.length == 0) {
throw new Error("addNotes() returned an empry result");
} else {
// console.log(result);
const failureWordsIndexList = result
.map((id, i) => {
console.log(id);
if (id === null) return i;
else return -1;
})
.filter((id) => id !== -1);
// console.log(`failureWordsIndexList: ${failureWordsIndexList}`);
if (failureWordsIndexList.length != 0) {
const failureWordsList = failureWordsIndexList.map(
(index) => wordsListForIndexing[index]
);
throw new Error(
"some words(" +
failureWordsIndexList.length +
") failed to add: [" +
failureWordsList.join(", ") +
"]"
);
}
results.forEach((result, i) => {
if (result.error) {
console.error("addNotes() error: ", JSON.stringify(result));
throw new Error(`word failed to add: ${notes[i].fields.Word}`);
}
});
}
return result;
return results;
} catch (error) {
writeToLog("[ERR] While adding notes:" + error);
}
Expand Down