Skip to content

Commit

Permalink
fix(user-permission): don't cache rejected promises (#1248)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeLi4L authored Jan 28, 2025
1 parent 0f5d148 commit 10bd993
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,24 @@ describe('UserPermission', () => {
expect(serverInterface.getUsers).toHaveBeenCalledTimes(2);
expect(userInfo).toEqual({ id: 43, email: '[email protected]' });
});

it('should not cache a rejected promise', async () => {
const { userPermissions, serverInterface } = setup();
serverInterface.getUsers = jest
.fn()
.mockRejectedValueOnce(new Error('first'))
.mockResolvedValueOnce([
{ id: 42, email: '[email protected]' },
{ id: 43, email: '[email protected]' },
]);

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: '[email protected]' });
});
});

describe('invalidateCache', () => {
Expand Down

0 comments on commit 10bd993

Please sign in to comment.