Skip to content

Commit

Permalink
Add Android Native handling of http error codes for failed requests
Browse files Browse the repository at this point in the history
mchowning committed Dec 19, 2019
1 parent 5b1872d commit a5e9eea
Showing 4 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion android/app/src/main/java/com/gutenberg/MainApplication.java
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class MainApplication extends Application implements ReactApplication {

@@ -114,7 +115,7 @@ public void editorDidEmitLog(String message, LogLevel logLevel) {
}

@Override
public void performRequest(String path, Consumer<String> onSuccess, Consumer<String> onError) {}
public void performRequest(String path, Consumer<String> onSuccess, Consumer<Map<String, Object>> onError) {}
});

return new ReactNativeHost(this) {
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

import androidx.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
@@ -191,7 +192,15 @@ public void getOtherMediaOptions(ReadableArray filter, final Callback jsCallback
public void fetchRequest(String path, Promise promise) {
mGutenbergBridgeJS2Parent.performRequest(path,
promise::resolve,
errorMessage -> promise.reject(new Error(errorMessage)));
errorMap -> {
Object code = errorMap.remove("code");
WritableMap writableMap = Arguments.makeNativeMap(errorMap);
if (code == null) {
promise.reject(new Error(), writableMap);
} else {
promise.reject(String.valueOf(code), new Error(), writableMap);
}
});
}

private OtherMediaOptionsReceivedCallback getNewOtherMediaReceivedCallback(final Callback jsCallback) {
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

import androidx.core.util.Consumer;

import java.util.Map;

public interface RequestExecutor {
void performRequest(String path, Consumer<String> onSuccess, Consumer<String> onError);
void performRequest(String path, Consumer<String> onSuccess, Consumer<Map<String, Object>> onError);
}
Original file line number Diff line number Diff line change
@@ -23,7 +23,6 @@
import com.facebook.react.ReactInstanceManagerBuilder;
import com.facebook.react.ReactPackage;
import com.facebook.react.ReactRootView;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.common.LifecycleState;
@@ -297,7 +296,7 @@ public void requestMediaPickFrom(String mediaSource,
}

@Override
public void performRequest(String pathFromJS, Consumer<String> onSuccess, Consumer<String> onError) {
public void performRequest(String pathFromJS, Consumer<String> onSuccess, Consumer<Map<String, Object>> onError) {
mRequestExecutor.performRequest(pathFromJS, onSuccess, onError);
}

0 comments on commit a5e9eea

Please sign in to comment.