Skip to content

Commit 09090b3

Browse files
committed
improve error returns from addNote...
avoid swallowed errors & false success result
1 parent cc913fb commit 09090b3

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

packages/db/src/impl/pouch/courseAPI.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,27 @@ export async function addNote55(
4040
});
4141

4242
if (result.ok) {
43-
// create cards
44-
await createCards(courseID, dataShapeId, result.id, tags, elo);
43+
try {
44+
// create cards
45+
await createCards(courseID, dataShapeId, result.id, tags, elo);
46+
} catch (error) {
47+
console.error(
48+
`[addNote55] Failed to create cards for note ${result.id}: ${
49+
error instanceof Error ? error.message : String(error)
50+
}`
51+
);
52+
// Add info to result to indicate card creation failed
53+
(result as any).cardCreationFailed = true;
54+
(result as any).cardCreationError = error instanceof Error ? error.message : String(error);
55+
}
4556
} else {
46-
console.log(`Error adding note: ${result}`);
57+
console.error(`[addNote55] Error adding note. Result: ${JSON.stringify(result)}`);
4758
}
4859

4960
return result;
5061
}
5162

52-
export async function createCards(
63+
async function createCards(
5364
courseID: string,
5465
datashapeID: PouchDB.Core.DocumentId,
5566
noteID: PouchDB.Core.DocumentId,
@@ -66,8 +77,16 @@ export async function createCards(
6677
}
6778
}
6879

80+
if (questionViewTypes.length === 0) {
81+
const errorMsg = `No questionViewTypes found for datashapeID: ${datashapeID} in course config. Cards cannot be created.`;
82+
console.error(errorMsg);
83+
throw new Error(errorMsg);
84+
}
85+
86+
let createdCards = 0;
6987
for (const questionView of questionViewTypes) {
70-
createCard(questionView, courseID, dsDescriptor, noteID, tags, elo);
88+
await createCard(questionView, courseID, dsDescriptor, noteID, tags, elo);
89+
createdCards++;
7190
}
7291
}
7392

@@ -86,7 +105,7 @@ async function createCard(
86105
for (const rQ of cfg.questionTypes) {
87106
if (rQ.name === questionViewName) {
88107
for (const view of rQ.viewList) {
89-
addCard(
108+
await addCard(
90109
courseID,
91110
dsDescriptor.course,
92111
[noteID],

packages/db/src/impl/pouch/courseDB.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,28 @@ above:\n${above.rows.map((r) => `\t${r.id}-${r.key}\n`)}`;
367367
try {
368368
const resp = await addNote55(this.id, codeCourse, shape, data, author, tags, uploads, elo);
369369
if (resp.ok) {
370+
// Check if card creation failed (property added by addNote55)
371+
if ((resp as any).cardCreationFailed) {
372+
console.warn(
373+
`[courseDB.addNote] Note added but card creation failed: ${
374+
(resp as any).cardCreationError
375+
}`
376+
);
377+
return {
378+
status: Status.error,
379+
message: `Note was added but no cards were created: ${(resp as any).cardCreationError}`,
380+
id: resp.id,
381+
};
382+
}
370383
return {
371384
status: Status.ok,
372385
message: '',
386+
id: resp.id,
373387
};
374388
} else {
375389
return {
376390
status: Status.error,
377-
message: 'Unexpected ',
391+
message: 'Unexpected error adding note',
378392
};
379393
}
380394
} catch (e) {
@@ -384,7 +398,7 @@ above:\n${above.rows.map((r) => `\t${r.id}-${r.key}\n`)}`;
384398
);
385399
return {
386400
status: Status.error,
387-
message: `Error adding note to course. ${(e as PouchError).reason}`,
401+
message: `Error adding note to course. ${(e as PouchError).reason || err.message}`,
388402
};
389403
}
390404
}

0 commit comments

Comments
 (0)