-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): Check for storage initialization errors (#13938)
* fix(core): Check for storage initialization errors * address nits and add logger * fixed unit test errors * bundle size fixes * implementing feedback Co-authored-by: AllanZhengYP <[email protected]> --------- Co-authored-by: AllanZhengYP <[email protected]>
- Loading branch information
1 parent
4458d30
commit 0f50917
Showing
5 changed files
with
86 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,51 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { ConsoleLogger } from '../Logger'; | ||
|
||
import { InMemoryStorage } from './InMemoryStorage'; | ||
|
||
/** | ||
* @internal | ||
* @returns Either a reference to window.localStorage or an in-memory storage as fallback | ||
*/ | ||
export const getLocalStorageWithFallback = (): Storage => | ||
typeof window !== 'undefined' && window.localStorage | ||
? window.localStorage | ||
: new InMemoryStorage(); | ||
|
||
const logger = new ConsoleLogger('CoreStorageUtils'); | ||
|
||
export const getLocalStorageWithFallback = (): Storage => { | ||
try { | ||
// Attempt to use localStorage directly | ||
if (typeof window !== 'undefined' && window.localStorage) { | ||
return window.localStorage; | ||
} | ||
} catch (e) { | ||
// Handle any errors related to localStorage access | ||
logger.error('LocalStorage access failed:', e); | ||
} | ||
|
||
// Return in-memory storage as a fallback if localStorage is not accessible | ||
return new InMemoryStorage(); | ||
}; | ||
|
||
/** | ||
* @internal | ||
* @returns Either a reference to window.sessionStorage or an in-memory storage as fallback | ||
*/ | ||
export const getSessionStorageWithFallback = (): Storage => | ||
typeof window !== 'undefined' && window.sessionStorage | ||
? window.sessionStorage | ||
: new InMemoryStorage(); | ||
export const getSessionStorageWithFallback = (): Storage => { | ||
try { | ||
// Attempt to use sessionStorage directly | ||
if (typeof window !== 'undefined' && window.sessionStorage) { | ||
// Verify we can actually use it by testing access | ||
window.sessionStorage.getItem('test'); | ||
|
||
return window.sessionStorage; | ||
} | ||
|
||
throw new Error('sessionStorage is not defined'); | ||
} catch (e) { | ||
// Handle any errors related to sessionStorage access | ||
logger.error('SessionStorage access failed:', e); | ||
|
||
return new InMemoryStorage(); | ||
} | ||
}; |