Skip to content

Commit c373529

Browse files
committed
fix(auth): guard setUserInfo in whoami against DB write failures
Cache update is a non-essential side effect — a read-only filesystem or corrupted DB must not prevent the command from displaying the user identity that was already successfully fetched from the API.
1 parent a1b7b8c commit c373529

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/commands/auth/whoami.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,18 @@ export const whoamiCommand = buildCommand({
4444

4545
const user = await getCurrentUser();
4646

47-
// Keep cached user info up to date
48-
setUserInfo({
49-
userId: user.id,
50-
email: user.email,
51-
username: user.username,
52-
name: user.name,
53-
});
47+
// Keep cached user info up to date. Non-fatal: display must succeed even
48+
// if the DB write fails (read-only filesystem, corrupted database, etc.).
49+
try {
50+
setUserInfo({
51+
userId: user.id,
52+
email: user.email,
53+
username: user.username,
54+
name: user.name,
55+
});
56+
} catch {
57+
// Cache update failure is non-essential — user identity was already fetched.
58+
}
5459

5560
if (flags.json) {
5661
writeJson(stdout, {

test/commands/auth/whoami.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ describe("whoamiCommand.func", () => {
165165
email: "jane@example.com",
166166
});
167167
});
168+
169+
test("still displays identity when DB cache write fails", async () => {
170+
isAuthenticatedSpy.mockResolvedValue(true);
171+
getCurrentUserSpy.mockResolvedValue(FULL_USER);
172+
setUserInfoSpy.mockImplementation(() => {
173+
throw new Error("read-only filesystem");
174+
});
175+
176+
const { context, getOutput } = createContext();
177+
// Must not throw — output must still be shown
178+
await func.call(context, { json: false });
179+
180+
expect(getOutput()).toContain("Jane Doe");
181+
});
168182
});
169183

170184
describe("--json output", () => {

0 commit comments

Comments
 (0)