mirror of
https://github.com/inaturalist/iNaturalistReactNative.git
synced 2025-12-23 22:18:36 -05:00
Migrate sensitive info error handling
This commit is contained in:
@@ -17,7 +17,7 @@ import { Alert, Platform } from "react-native";
|
||||
import Config from "react-native-config";
|
||||
import * as RNLocalize from "react-native-localize";
|
||||
import RNRestart from "react-native-restart";
|
||||
import RNSInfo from "react-native-sensitive-info";
|
||||
import RNSInfo, { isSensitiveInfoError, SensitiveInfoError } from "react-native-sensitive-info";
|
||||
import Realm, { UpdateMode } from "realm";
|
||||
import realmConfig from "realmModels/index";
|
||||
import changeLanguage from "sharedHelpers/changeLanguage";
|
||||
@@ -25,7 +25,7 @@ import { getInstallID } from "sharedHelpers/installData";
|
||||
import { log, logFilePath, logWithoutRemote } from "sharedHelpers/logger";
|
||||
import removeAllFilesFromDirectory from "sharedHelpers/removeAllFilesFromDirectory";
|
||||
import safeRealmWrite from "sharedHelpers/safeRealmWrite";
|
||||
import { sleep, unlink } from "sharedHelpers/util";
|
||||
import { unlink } from "sharedHelpers/util";
|
||||
import { isDebugMode } from "sharedHooks/useDebugMode";
|
||||
import { storage } from "stores/useStore";
|
||||
|
||||
@@ -68,42 +68,51 @@ const clearAuthCache = ( ): void => {
|
||||
};
|
||||
|
||||
async function getSensitiveItem( key: string, options = {} ) {
|
||||
const exists = await RNSInfo.hasItem( key, options );
|
||||
if ( !exists ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return await RNSInfo.getItem( key, options );
|
||||
} catch ( e ) {
|
||||
if ( isDebugMode() ) {
|
||||
localLogger.info( `RNSInfo.getItem not available for ${key}, sleeping` );
|
||||
if ( isSensitiveInfoError( e ) ) {
|
||||
const getItemError = e as SensitiveInfoError;
|
||||
if ( isDebugMode() ) {
|
||||
switch ( getItemError.code ) {
|
||||
case "E_NOT_FOUND":
|
||||
// Value doesn't exist
|
||||
localLogger.info( `RNSInfo.getItem not available for ${key}` );
|
||||
break;
|
||||
default:
|
||||
localLogger.info( `RNSInfo.getItem unknown error for ${key}: ${getItemError.message}` );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
const getItemError = e as Error;
|
||||
if ( getItemError.message.match( /Protected data not available yet/ ) ) {
|
||||
await sleep( 1_000 );
|
||||
return RNSInfo.getItem( key, options );
|
||||
}
|
||||
if ( isDebugMode() ) {
|
||||
localLogger.info( `RNSInfo.getItem not available for ${key} after sleeping, throwing` );
|
||||
}
|
||||
throw getItemError;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async function setSensitiveItem( key: string, value: string, options = {} ) {
|
||||
const actualOptions = {
|
||||
...options,
|
||||
accessControl: "none" as const
|
||||
};
|
||||
try {
|
||||
const result = await RNSInfo.setItem( key, value, options );
|
||||
const result = await RNSInfo.setItem( key, value, actualOptions );
|
||||
clearAuthCache( );
|
||||
return result;
|
||||
} catch ( e ) {
|
||||
if ( isDebugMode( ) ) {
|
||||
localLogger.info( `RNSInfo.setItem not available for ${key}, sleeping` );
|
||||
if ( isSensitiveInfoError( e ) ) {
|
||||
const setItemError = e as SensitiveInfoError;
|
||||
if ( isDebugMode( ) ) {
|
||||
localLogger.info(
|
||||
`RNSInfo.setItem error for ${key}, ${setItemError.code} ${setItemError.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
const setItemError = e as Error;
|
||||
if ( setItemError.message.match( /Protected data not available yet/ ) ) {
|
||||
await sleep( 1_000 );
|
||||
return RNSInfo.setItem( key, value, options );
|
||||
}
|
||||
if ( isDebugMode( ) ) {
|
||||
localLogger.info( `RNSInfo.setItem not available for ${key} after sleeping, throwing` );
|
||||
}
|
||||
throw setItemError;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,18 +122,15 @@ async function deleteSensitiveItem( key: string, options = {} ) {
|
||||
clearAuthCache( );
|
||||
return result;
|
||||
} catch ( e ) {
|
||||
if ( isDebugMode( ) ) {
|
||||
localLogger.info( `RNSInfo.deleteItem not available for ${key}, sleeping` );
|
||||
if ( isSensitiveInfoError( e ) ) {
|
||||
const deleteItemError = e as SensitiveInfoError;
|
||||
if ( isDebugMode() ) {
|
||||
localLogger.info(
|
||||
`RNSInfo.deleteItem error for ${key}, ${deleteItemError.code} ${deleteItemError.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
const deleteItemError = e as Error;
|
||||
if ( deleteItemError.message.match( /Protected data not available yet/ ) ) {
|
||||
await sleep( 500 );
|
||||
return RNSInfo.deleteItem( key, options );
|
||||
}
|
||||
if ( isDebugMode( ) ) {
|
||||
localLogger.info( `RNSInfo.deleteItem not available after sleeping for ${key}, throwing` );
|
||||
}
|
||||
throw deleteItemError;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user