Skip to content

Commit 2f43e57

Browse files
committed
fix: capture proper stack traces for tasks
1 parent c1e0887 commit 2f43e57

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/lib/db.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ async function fsyncDir(dirname: string): Promise<void> {
137137
await fs.close(fd);
138138
}
139139

140+
function getCurrentErrorStack(): string {
141+
const tmp = { message: "" };
142+
Error.captureStackTrace(tmp);
143+
return (tmp as any).stack.split("\n").slice(2).join("\n");
144+
}
145+
140146
export class JsonlDB<V extends unknown = unknown> {
141147
public constructor(filename: string, options: JsonlDBOptions<V> = {}) {
142148
this.validateOptions(options);
@@ -640,7 +646,13 @@ export class JsonlDB<V extends unknown = unknown> {
640646
filename: targetFilename,
641647
done,
642648
});
643-
return done;
649+
const stack = getCurrentErrorStack();
650+
try {
651+
await done;
652+
} catch (e: any) {
653+
e.stack += "\n" + stack;
654+
throw e;
655+
}
644656
}
645657

646658
private needToCompressBySize(): boolean {
@@ -864,7 +876,13 @@ export class JsonlDB<V extends unknown = unknown> {
864876
type: "compress",
865877
done,
866878
});
867-
return done;
879+
const stack = getCurrentErrorStack();
880+
try {
881+
await done;
882+
} catch (e: any) {
883+
e.stack += "\n" + stack;
884+
throw e;
885+
}
868886
}
869887

870888
/** Closes the DB and waits for all data to be written */

0 commit comments

Comments
 (0)