From e450fe43932d34808528f2554e514615b405bfd3 Mon Sep 17 00:00:00 2001 From: Dave Miller Date: Tue, 19 Jan 2016 12:09:01 -0800 Subject: [PATCH] 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 --- .../com/facebook/react/bridge/Promise.java | 3 +++ .../facebook/react/bridge/PromiseImpl.java | 22 +++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/Promise.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/Promise.java index 92ce60699ce807..84b802b28affae 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/Promise.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/Promise.java @@ -19,5 +19,8 @@ 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); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/PromiseImpl.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/PromiseImpl.java index d6e25b694fb9bf..efbc4537592d52 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/PromiseImpl.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/PromiseImpl.java @@ -15,6 +15,9 @@ 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 void resolve(Object value) { @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); } }