Skip to content

Commit

Permalink
Standardize Error objects for Promises
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Dave Miller authored and christopherdro committed Jan 20, 2016
1 parent 3b86434 commit e450fe4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
Expand Down

0 comments on commit e450fe4

Please sign in to comment.