From 10bd993c62920286e244436ac31bc614bc4bc34e Mon Sep 17 00:00:00 2001 From: Valentin Vetter Date: Tue, 28 Jan 2025 14:33:02 +0100 Subject: [PATCH] fix(user-permission): don't cache rejected promises (#1248) --- .../src/permissions/user-permission.ts | 7 ++++++- .../test/permissions/user-permission.test.ts | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/forestadmin-client/src/permissions/user-permission.ts b/packages/forestadmin-client/src/permissions/user-permission.ts index 7393bb2605..501ab8dc6d 100644 --- a/packages/forestadmin-client/src/permissions/user-permission.ts +++ b/packages/forestadmin-client/src/permissions/user-permission.ts @@ -31,7 +31,12 @@ export default class UserPermissionService { // the response is not yet available. this.userInfoById = this.forestAdminServerInterface .getUsers(this.options) - .then(users => new Map(users.map(user => [`${user.id}`, user]))); + .then(users => new Map(users.map(user => [`${user.id}`, user]))) + .catch(err => { + // Don't cache rejected promises + this.invalidateCache(); + throw err; + }); } return (await this.userInfoById).get(`${userId}`); diff --git a/packages/forestadmin-client/test/permissions/user-permission.test.ts b/packages/forestadmin-client/test/permissions/user-permission.test.ts index d36e850e7f..42b68a8122 100644 --- a/packages/forestadmin-client/test/permissions/user-permission.test.ts +++ b/packages/forestadmin-client/test/permissions/user-permission.test.ts @@ -143,6 +143,24 @@ describe('UserPermission', () => { expect(serverInterface.getUsers).toHaveBeenCalledTimes(2); expect(userInfo).toEqual({ id: 43, email: 'bob2@world.com' }); }); + + it('should not cache a rejected promise', async () => { + const { userPermissions, serverInterface } = setup(); + serverInterface.getUsers = jest + .fn() + .mockRejectedValueOnce(new Error('first')) + .mockResolvedValueOnce([ + { id: 42, email: 'alice@world.com' }, + { id: 43, email: 'bob2@world.com' }, + ]); + + await expect(() => userPermissions.getUserInfo(43)).rejects.toThrow('first'); + + const userInfo = await userPermissions.getUserInfo(43); + + expect(serverInterface.getUsers).toHaveBeenCalledTimes(2); + expect(userInfo).toEqual({ id: 43, email: 'bob2@world.com' }); + }); }); describe('invalidateCache', () => {