Skip to content

Commit 71c6777

Browse files
committed
AUT-5433: Return combined error when both session store reads fail
Adds DualStoreError with typed primary and secondary properties so callers can identify which stores failed easier.
1 parent 036f352 commit 71c6777

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/config/dual-session-store.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { SessionData, Store } from "express-session";
22
import { logger } from "../utils/logger.js";
33
import { isDeepStrictEqual } from "util";
44

5+
export class DualStoreError extends Error {
6+
constructor(
7+
public readonly primary: Error,
8+
public readonly secondary: Error
9+
) {
10+
super("Both session stores failed");
11+
}
12+
}
13+
514
export class DualSessionStore extends Store {
615
constructor(
716
private primary: Store,
@@ -23,7 +32,10 @@ export class DualSessionStore extends Store {
2332

2433
this.secondary.get(sid, (secondaryErr, secondarySession) => {
2534
if (primaryErr) {
26-
cb(secondaryErr, secondarySession);
35+
const error = secondaryErr
36+
? new DualStoreError(primaryErr, secondaryErr)
37+
: null;
38+
cb(error, secondarySession);
2739
return;
2840
}
2941

test/unit/dual-session-store.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterEach, beforeEach, describe, it } from "mocha";
22
import { expect, sinon } from "../utils/test-utils.js";
3-
import { DualSessionStore } from "../../src/config/dual-session-store.js";
3+
import { DualSessionStore, DualStoreError } from "../../src/config/dual-session-store.js";
44
import { SessionData, Store } from "express-session";
55

66
describe("DualSessionStore", () => {
@@ -45,14 +45,18 @@ describe("DualSessionStore", () => {
4545
});
4646
});
4747

48-
it("should return secondary error when both primary and secondary fail", (done) => {
48+
it("should return combined error when both primary and secondary fail", (done) => {
49+
const primaryErr = new Error("primary failure");
4950
const secondaryErr = new Error("secondary failure");
50-
primary.get = sinon.fake((_sid, cb) => cb(new Error("primary failure")));
51+
primary.get = sinon.fake((_sid, cb) => cb(primaryErr));
5152
secondary.get = sinon.fake((_sid, cb) => cb(secondaryErr));
5253
store = new DualSessionStore(primary, secondary, "Redis", "DynamoDB");
5354

5455
store.get(sid, (err) => {
55-
expect(err).to.equal(secondaryErr);
56+
expect(err).to.be.instanceOf(DualStoreError);
57+
expect(err.message).to.equal("Both session stores failed");
58+
expect(err.primary).to.equal(primaryErr);
59+
expect(err.secondary).to.equal(secondaryErr);
5660
done();
5761
});
5862
});

0 commit comments

Comments
 (0)