Skip to content

feat: add exception when array is passed for 2nd arg #573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/AsyncStorage.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ function checkValidInput(usedKey: string, value: any) {
}
}

function checkValidArgs(keyValuePairs, callback) {
if (
!Array.isArray(keyValuePairs) ||
keyValuePairs.length === 0 ||
!Array.isArray(keyValuePairs[0])
) {
throw new Error(
'[AsyncStorage] Expected array of key-value pairs as first argument to multiSet',
);
}

if (callback && typeof callback !== 'function') {
if (Array.isArray(callback)) {
throw new Error(
'[AsyncStorage] Expected function as second argument to multiSet. Did you forget to wrap key-value pairs in an array for the first argument?',
);
}

throw new Error(
'[AsyncStorage] Expected function as second argument to multiSet',
);
}
}

/**
* `AsyncStorage` is a simple, unencrypted, asynchronous, persistent, key-value
* storage system that is global to the app. It should be used instead of
Expand Down Expand Up @@ -321,6 +345,7 @@ const AsyncStorage = {
keyValuePairs: Array<Array<string>>,
callback?: ?(errors: ?$ReadOnlyArray<?Error>) => void,
): Promise<null> {
checkValidArgs(keyValuePairs, callback);
return new Promise((resolve, reject) => {
keyValuePairs.forEach(([key, value]) => {
checkValidInput(key, value);
Expand Down