@@ -475,14 +475,9 @@ function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>(collect
475475 * @returns true if the key is a collection member, false otherwise
476476 */
477477function isCollectionMember ( key : OnyxKey ) : boolean {
478- try {
479- const collectionKey = getCollectionKey ( key ) ;
480- // If the key is longer than the collection key, it's a collection member
481- return key . length > collectionKey . length ;
482- } catch ( e ) {
483- // If getCollectionKey throws, the key is not a collection member
484- return false ;
485- }
478+ const collectionKey = getCollectionKey ( key ) ;
479+ // If the key is longer than the collection key, it's a collection member
480+ return ! ! collectionKey && key . length > collectionKey . length ;
486481}
487482
488483/**
@@ -503,12 +498,9 @@ function isCollectionMember(key: OnyxKey): boolean {
503498 * @returns true if key is a RAM-only key, RAM-only collection key, or a RAM-only collection member
504499 */
505500function isRamOnlyKey ( key : OnyxKey ) : boolean {
506- try {
507- const collectionKey = getCollectionKey ( key ) ;
508- // If collectionKey exists for a given key, check if it's a RAM-only key
501+ const collectionKey = getCollectionKey ( key ) ;
502+ if ( collectionKey ) {
509503 return cache . isRamOnlyKey ( collectionKey ) ;
510- } catch {
511- // If getCollectionKey throws, the key is not a collection member
512504 }
513505
514506 return cache . isRamOnlyKey ( key ) ;
@@ -530,8 +522,12 @@ function splitCollectionMemberKey<TKey extends CollectionKey, CollectionKeyType
530522 }
531523
532524 if ( ! collectionKey ) {
525+ const resolvedKey = getCollectionKey ( key ) ;
526+ if ( ! resolvedKey ) {
527+ throw new Error ( `Invalid '${ key } ' key provided, only collection keys are allowed.` ) ;
528+ }
533529 // eslint-disable-next-line no-param-reassign
534- collectionKey = getCollectionKey ( key ) ;
530+ collectionKey = resolvedKey ;
535531 }
536532
537533 return [ collectionKey as CollectionKeyType , key . slice ( collectionKey . length ) ] ;
@@ -555,9 +551,9 @@ function isKeyMatch(configKey: OnyxKey, key: OnyxKey): boolean {
555551 * - `getCollectionKey("sharedNVP_user_-1_something")` would return "sharedNVP_user_"
556552 *
557553 * @param key - The collection key to process.
558- * @returns The plain collection key or throws an Error if the key is not a collection one.
554+ * @returns The plain collection key or undefined if the key is not a collection one.
559555 */
560- function getCollectionKey ( key : CollectionKey ) : string {
556+ function getCollectionKey ( key : CollectionKey ) : string | undefined {
561557 // Start by finding the position of the last underscore in the string
562558 let lastUnderscoreIndex = key . lastIndexOf ( '_' ) ;
563559
@@ -575,7 +571,7 @@ function getCollectionKey(key: CollectionKey): string {
575571 lastUnderscoreIndex = key . lastIndexOf ( '_' , lastUnderscoreIndex - 1 ) ;
576572 }
577573
578- throw new Error ( `Invalid ' ${ key } ' key provided, only collection keys are allowed.` ) ;
574+ return undefined ;
579575}
580576
581577/**
@@ -754,13 +750,7 @@ function keyChanged<TKey extends OnyxKey>(
754750 // do the same in keysChanged, because we only call that function when a collection key changes, and it doesn't happen that often.
755751 // For performance reason, we look for the given key and later if don't find it we look for the collection key, instead of checking if it is a collection key first.
756752 let stateMappingKeys = onyxKeyToSubscriptionIDs . get ( key ) ?? [ ] ;
757- let collectionKey : string | undefined ;
758- try {
759- collectionKey = getCollectionKey ( key ) ;
760- } catch ( e ) {
761- // If getCollectionKey() throws an error it means the key is not a collection key.
762- collectionKey = undefined ;
763- }
753+ const collectionKey = getCollectionKey ( key ) ;
764754
765755 if ( collectionKey ) {
766756 // Getting the collection key from the specific key because only collection keys were stored in the mapping.
0 commit comments