[Onyx Audit] Remove try/catch block from getCollectionKey#737
Draft
JKobrynski wants to merge 2 commits intoExpensify:mainfrom
Draft
Conversation
fabioh8010
reviewed
Feb 18, 2026
| // If getCollectionKey() throws an error it means the key is not a collection key. | ||
| collectionKey = undefined; | ||
| } | ||
| const collectionKey = OnyxUtils.getCollectionKey(key) ?? undefined; |
Contributor
There was a problem hiding this comment.
Suggested change
| const collectionKey = OnyxUtils.getCollectionKey(key) ?? undefined; | |
| const collectionKey = OnyxUtils.getCollectionKey(key); |
| const collectionBaseKey = OnyxUtils.getCollectionKey(keyToInvalidate); | ||
| // Check if the key is a collection member and invalidate the collection base key | ||
| const collectionBaseKey = OnyxUtils.getCollectionKey(keyToInvalidate); | ||
| if (collectionBaseKey !== null) { |
Contributor
There was a problem hiding this comment.
Suggested change
| if (collectionBaseKey !== null) { | |
| if (collectionBaseKey) { |
| } | ||
| const collectionKey = getCollectionKey(key); | ||
| // If the key is longer than the collection key, it's a collection member | ||
| return collectionKey !== null && key.length > collectionKey.length; |
Contributor
There was a problem hiding this comment.
Suggested change
| return collectionKey !== null && key.length > collectionKey.length; | |
| return collectionKey !== undefined && key.length > collectionKey.length; |
| const collectionKey = getCollectionKey(key); | ||
| // If collectionKey exists for a given key, check if it's a RAM-only key | ||
| const collectionKey = getCollectionKey(key); | ||
| if (collectionKey !== null) { |
Contributor
There was a problem hiding this comment.
Suggested change
| if (collectionKey !== null) { | |
| if (collectionKey) { |
|
|
||
| if (!collectionKey) { | ||
| const resolvedKey = getCollectionKey(key); | ||
| if (resolvedKey === null) { |
Contributor
There was a problem hiding this comment.
Suggested change
| if (resolvedKey === null) { | |
| if (!resolvedKey) { |
| * @returns The plain collection key or null if the key is not a collection one. | ||
| */ | ||
| function getCollectionKey(key: CollectionKey): string { | ||
| function getCollectionKey(key: CollectionKey): string | null { |
Contributor
There was a problem hiding this comment.
Suggested change
| function getCollectionKey(key: CollectionKey): string | null { | |
| function getCollectionKey(key: CollectionKey): string | undefined { |
It's much simpler if we use undefined instead of null
| } | ||
|
|
||
| throw new Error(`Invalid '${key}' key provided, only collection keys are allowed.`); | ||
| return null; |
Contributor
There was a problem hiding this comment.
Suggested change
| return null; | |
| return undefined; |
| // If getCollectionKey() throws an error it means the key is not a collection key. | ||
| collectionKey = undefined; | ||
| } | ||
| const collectionKey = getCollectionKey(key) ?? undefined; |
Contributor
There was a problem hiding this comment.
Suggested change
| const collectionKey = getCollectionKey(key) ?? undefined; | |
| const collectionKey = getCollectionKey(key); |
|
|
||
| it('should invalidate non-collection keys without affecting others', () => { | ||
| mockedOnyxUtils.isCollectionKey.mockReturnValue(false); | ||
| mockedOnyxUtils.getCollectionKey.mockReturnValue(null); |
Contributor
There was a problem hiding this comment.
Suggested change
| mockedOnyxUtils.getCollectionKey.mockReturnValue(null); | |
| mockedOnyxUtils.getCollectionKey.mockReturnValue(undefined); |
Comment on lines
+346
to
+347
| expect(OnyxUtils.getCollectionKey(ONYXKEYS.TEST_KEY)).toBeNull(); | ||
| expect(OnyxUtils.getCollectionKey('')).toBeNull(); |
Contributor
There was a problem hiding this comment.
Suggested change
| expect(OnyxUtils.getCollectionKey(ONYXKEYS.TEST_KEY)).toBeNull(); | |
| expect(OnyxUtils.getCollectionKey('')).toBeNull(); | |
| expect(OnyxUtils.getCollectionKey(ONYXKEYS.TEST_KEY)).toBeUndefined(); | |
| expect(OnyxUtils.getCollectionKey('')).toBeUndefined(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Details
Related Issues
GH_LINK Expensify/App#81830
Automated Tests
Unit test covering related logic was added.
Manual Tests
As this is a minor change that only slightly alters how the library handles some edge cases, we just need to test the basic flows of the app and make sure everything works correctly, especially the following:
Everything worked correctly in my tests.
Author Checklist
### Related Issuessection aboveTestssectiontoggleReportand notonIconClick)myBool && <MyComponent />.STYLE.md) were followedAvatar, I verified the components usingAvatarare working as expected)/** comment above it */thisproperly so there are no scoping issues (i.e. foronClick={this.submit}the methodthis.submitshould be bound tothisin the constructor)thisare necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);ifthis.submitis never passed to a component event handler likeonClick)Avataris modified, I verified thatAvataris working as expected in all cases)mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.Screenshots/Videos
Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
web-compressed.mov
MacOS: Desktop