Files
react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/PromiseImpl.java
David Aurelio a68c731aca Android: Accept Throwables in Promise.reject()
Summary:
public

Fixes #4309

This adds the possibility to reject `Promise` instances with `Throwable`s in java, instead of strings.
For now, it only reads the message, but we can add more features on top of this, e.g. forwarding the error stack.

Reviewed By: andreicoman11

Differential Revision: D2708192

fb-gh-sync-id: ca5ff584eca29370a9f9b780fa9825b17863a7e9
2015-12-03 04:52:26 -08:00

51 lines
1.4 KiB
Java

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/**
* Implementation of two javascript functions that can be used to resolve or reject a js promise.
*/
package com.facebook.react.bridge;
import javax.annotation.Nullable;
public class PromiseImpl implements Promise {
private @Nullable Callback mResolve;
private @Nullable Callback mReject;
public PromiseImpl(@Nullable Callback resolve, @Nullable Callback reject) {
mResolve = resolve;
mReject = reject;
}
@Override
public void resolve(Object value) {
if (mResolve != null) {
mResolve.invoke(value);
}
}
@Override
public void reject(Throwable reason) {
reject(reason.getMessage());
}
@Override
public void reject(String reason) {
if (mReject != null) {
// The JavaScript side expects a map with at least the error message.
// It is possible to expose all kind of information. It will be available on the JS
// error instance.
// TODO(8850038): add more useful information, e.g. the native stack trace.
WritableNativeMap errorInfo = new WritableNativeMap();
errorInfo.putString("message", reason);
mReject.invoke(errorInfo);
}
}
}