Skip to content

Commit

Permalink
[error-recovery] Add js side serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmccall committed Aug 28, 2019
1 parent 79ba500 commit fd4a8fd
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ class ScopedErrorRecoveryModule(context: Context, val experienceId: ExperienceId
mSharedPreferences.edit().putString(experienceId.get(), props).apply()
}

override fun popProps(): Map<String, Any> {
with(mSharedPreferences.getString(experienceId.get(), "")) {
return if (isNotEmpty()) {
mSharedPreferences.edit().remove(experienceId.get()).apply()
getPropsFromString(this)
} else {
emptyMap()
}
override fun popProps(): String? {
return mSharedPreferences.getString(experienceId.get(), null)?.let {
mSharedPreferences.edit().remove(experienceId.get()).apply()
it
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ - (instancetype)initWithExperienceId:(NSString *)experienceId
return self;
}

- (BOOL)pushProps:(NSDictionary *)props
- (BOOL)pushProps:(NSString *)props
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSDictionary *errorRecoveryStore = [preferences objectForKey:userDefaultsKey];
Expand All @@ -36,12 +36,12 @@ - (BOOL)pushProps:(NSDictionary *)props
}
}

- (NSDictionary *)popProps
- (NSString *)popProps
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSDictionary *errorRecoveryStore = [preferences objectForKey:userDefaultsKey];
if (errorRecoveryStore != nil) {
NSDictionary *props = [errorRecoveryStore objectForKey:_experienceId];
NSString *props = [errorRecoveryStore objectForKey:_experienceId];
if (props != nil) {
NSMutableDictionary *storeWithRemovedProps = [errorRecoveryStore mutableCopy];
[storeWithRemovedProps removeObjectForKey:_experienceId];
Expand Down
2 changes: 1 addition & 1 deletion packages/expo-error-recovery/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# expo-error-recovery

`expo-error-recovery` helping you gracefully handle crashes due to fatal JavaScript errors.
`expo-error-recovery` helps you gracefully handle crashes caused by fatal JavaScript errors.

# API documentation

Expand Down
12 changes: 7 additions & 5 deletions packages/expo-error-recovery/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ apply plugin: 'kotlin-android'
group = 'host.exp.exponent'
version = '1.0.0'

def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

// Upload android library to maven with javadoc and android sources
configurations {
deployerJars
Expand All @@ -42,11 +46,11 @@ uploadArchives {
}

android {
compileSdkVersion 26
compileSdkVersion safeExtGet("compileSdkVersion", 28)

defaultConfig {
minSdkVersion 21
targetSdkVersion 26
minSdkVersion safeExtGet("minSdkVersion", 21)
targetSdkVersion safeExtGet("targetSdkVersion", 28)
versionCode 1
versionName '1.0.0'
}
Expand All @@ -67,6 +71,4 @@ dependencies {
unimodule 'unimodules-core'

api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"

implementation 'com.google.code.gson:gson:2.8.5'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package expo.modules.errorrecovery

import android.content.Context
import android.content.SharedPreferences
import com.google.gson.Gson
import com.google.gson.JsonIOException
import com.google.gson.reflect.TypeToken

import org.unimodules.core.ExportedModule
import org.unimodules.core.ModuleRegistry
Expand All @@ -19,19 +16,14 @@ open class ErrorRecoveryModule(context: Context) : ExportedModule(context) {

override fun getName(): String = "ExpoErrorRecovery"


override fun onCreate(moduleRegistry: ModuleRegistry) {
mSharedPreferences = context.applicationContext.getSharedPreferences(ERROR_STORE, Context.MODE_PRIVATE)
}

@ExpoMethod
fun setRecoveryProps(props: Map<String, Any>, promise: Promise) {
return try {
propsReadyToSave = Gson().toJson(props)
promise.resolve(null)
} catch (exception: JsonIOException) {
promise.reject("E_INVALID_PROPS", "Cannot parse props.", exception)
}
fun setRecoveryProps(props: String, promise: Promise) {
propsReadyToSave = props
promise.resolve(null)
}

@ExpoMethod
Expand All @@ -42,34 +34,19 @@ open class ErrorRecoveryModule(context: Context) : ExportedModule(context) {
promise.resolve(null)
}

override fun getConstants(): Map<String, Any>? {
popProps().let {
return if (it.isEmpty()) {
null
} else {
mapOf("errors" to it)
}
}
override fun getConstants(): Map<String, Any?> {
return mapOf("errors" to popProps())
}

protected fun getPropsFromString(propsString: String): Map<String, Any> {
return Gson().fromJson(propsString, object : TypeToken<Map<String, Any>>() {}.type)
}

protected open fun pushProps(props: String) {
mSharedPreferences.edit().putString("errorRecovery", props).apply()
}

protected open fun popProps(): Map<String, Any?> {
with(mSharedPreferences.getString("errorRecovery", "")) {
return if (isNotEmpty()) {
mSharedPreferences.edit().remove("errorRecovery").apply()
getPropsFromString(this)
} else {
emptyMap()
}
protected open fun popProps(): String? {
return mSharedPreferences.getString("errorRecovery", null)?.let {
mSharedPreferences.edit().remove("errorRecovery").apply()
it
}
}


}
14 changes: 10 additions & 4 deletions packages/expo-error-recovery/build/ErrorRecovery.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/expo-error-recovery/build/ErrorRecovery.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

@interface EXErrorRecoveryModule : UMExportedModule

- (BOOL)pushProps:(NSDictionary *)props;
- (BOOL)pushProps:(NSString *)props;

- (NSDictionary *)popProps;
- (NSString *)popProps;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@interface EXErrorRecoveryModule ()

@property (nonatomic, strong) NSDictionary *recoveryPropsToSave;
@property (nonatomic, strong) NSString *recoveryPropsToSave;

@end

Expand All @@ -15,7 +15,7 @@ @implementation EXErrorRecoveryModule
UM_EXPORT_MODULE(ExpoErrorRecovery);

UM_EXPORT_METHOD_AS(setRecoveryProps,
setRecoveryProps:(NSDictionary *)props
setRecoveryProps:(NSString *)props
resovler:(UMPromiseResolveBlock)resolve
rejecter:(UMPromiseRejectBlock)reject)
{
Expand Down Expand Up @@ -43,16 +43,16 @@ - (NSDictionary *)constantsToExport
};
}

- (BOOL)pushProps:(NSDictionary *)props {
- (BOOL)pushProps:(NSString *)props {
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
[preferences setObject:props forKey:userDefaultsKey];
return [preferences synchronize];
}

- (NSDictionary *)popProps
- (NSString *)popProps
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSDictionary *props = [preferences objectForKey:userDefaultsKey];
NSString *props = [preferences objectForKey:userDefaultsKey];
if (props != nil) {
[preferences removeObjectForKey:userDefaultsKey];
[preferences synchronize];
Expand Down
2 changes: 1 addition & 1 deletion packages/expo-error-recovery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react-native",
"expo",
"expo-error-recovery",
"errorr-recovery"
"error-recovery"
],
"repository": {
"type": "git",
Expand Down
15 changes: 11 additions & 4 deletions packages/expo-error-recovery/src/ErrorRecovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { UnavailabilityError } from '@unimodules/core';
import { once } from 'lodash';
import ExpoErrorRecovery from './ExpoErrorRecovery';

const globalHadlerSwapper = once(() => {
const globalHandlerSwapper = once(() => {
// ErrorUtlis came from react-native
// https://github.com/facebook/react-native/blob/1151c096dab17e5d9a6ac05b61aacecd4305f3db/Libraries/vendor/core/ErrorUtils.js#L25
const globalHandler = ErrorUtils.getGlobalHandler();
Expand All @@ -12,12 +12,19 @@ const globalHadlerSwapper = once(() => {
});
});

export const errors = ExpoErrorRecovery.errors;
export const errors = _parseNativeErrors();

export function setRecoveryProps(props: { [key: string]: any }): void {
if (!ExpoErrorRecovery.setRecoveryProps) {
throw new UnavailabilityError('ErrorRecovery', 'setRecoveryProps');
}
ExpoErrorRecovery.setRecoveryProps(props);
globalHadlerSwapper();
ExpoErrorRecovery.setRecoveryProps(JSON.stringify(props));
globalHandlerSwapper();
}

function _parseNativeErrors() {
if (ExpoErrorRecovery.errors) {
return JSON.parse(ExpoErrorRecovery.errors);
}
return undefined;
}
12 changes: 0 additions & 12 deletions packages/expo/src/__tests__/ErrorRecovery-test.ts

This file was deleted.

0 comments on commit fd4a8fd

Please sign in to comment.