Standardize Error objects for Promises

Summary:
public

Promises are coming.  And as part of it, we are standardizing the error objects that will be returned.  This puts the code in place on the Android side to always send the proper error format.

It will be an error object like this
`{
  code : "E_SOME_ERROR_CODE_DEFINED_BY_MODULE", // Meant to be machine parseable
  message : "Human readable message",
  nativeError : {} // Some representation of the underlying error (Exception or NSError) , still figuring out exactly, but hopefully something with stack info
}`

Reviewed By: davidaurelio

Differential Revision: D2839927

fb-gh-sync-id: 08f1ce79af24d70357b9957f14471a12627dcffa
This commit is contained in:
Dave Miller 2016-01-19 12:09:01 -08:00 committed by facebook-github-bot-7
parent c8355d377f
commit a10b4d8c47
2 changed files with 23 additions and 2 deletions

View File

@ -19,5 +19,8 @@ package com.facebook.react.bridge;
public interface Promise {
void resolve(Object value);
void reject(Throwable reason);
@Deprecated
void reject(String reason);
void reject(String code, Throwable extra);
void reject(String code, String reason, Throwable extra);
}

View File

@ -15,6 +15,9 @@ package com.facebook.react.bridge;
import javax.annotation.Nullable;
public class PromiseImpl implements Promise {
private static final String DEFAULT_ERROR = "EUNSPECIFIED";
private @Nullable Callback mResolve;
private @Nullable Callback mReject;
@ -32,18 +35,33 @@ public class PromiseImpl implements Promise {
@Override
public void reject(Throwable reason) {
reject(reason.getMessage());
reject(DEFAULT_ERROR, reason.getMessage(), reason);
}
@Override
@Deprecated
public void reject(String reason) {
reject(DEFAULT_ERROR, reason, null);
}
@Override
public void reject(String code, Throwable extra) {
reject(code, extra.getMessage(), extra);
}
@Override
public void reject(String code, String reason, Throwable extra) {
if (mReject != null) {
if (code == null) {
code = DEFAULT_ERROR;
}
// 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("code", code);
errorInfo.putString("message", reason);
// TODO(8850038): add the stack trace info in, need to figure out way to serialize that
mReject.invoke(errorInfo);
}
}