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
  • Loading branch information
mchowning committed Dec 19, 2019
1 parent 5b1872d commit c566ac0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit c566ac0

Please sign in to comment.