Skip to content

Commit

Permalink
Merge pull request #7 from Serifold/error
Browse files Browse the repository at this point in the history
refactor: improve the output of cards' error info
  • Loading branch information
Quasimurdock authored Aug 8, 2023
2 parents 14ba0f2 + d98dc0b commit 088698e
Showing 1 changed file with 17 additions and 28 deletions.
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

0 comments on commit 088698e

Please sign in to comment.