-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Promise): Adding userInfo to reject error object (#732)
Using System.Exception.Data IDictionary, users can add arbitrary data to a "userInfo" property on the error object. Fixes #730
- Loading branch information
Showing
5 changed files
with
160 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
|
||
namespace ReactNative.Bridge | ||
{ | ||
class Promise : IPromise | ||
{ | ||
private const string DefaultError = "EUNSPECIFIED"; | ||
|
||
private readonly ICallback _resolve; | ||
private readonly ICallback _reject; | ||
|
||
public Promise(ICallback resolve, ICallback reject) | ||
{ | ||
_resolve = resolve; | ||
_reject = reject; | ||
} | ||
|
||
public void Resolve(object value) | ||
{ | ||
if (_resolve != null) | ||
{ | ||
_resolve.Invoke(value); | ||
} | ||
} | ||
|
||
public void Reject(string code, string message) | ||
{ | ||
Reject(code, message, default(Exception)); | ||
} | ||
|
||
public void Reject(string message) | ||
{ | ||
Reject(DefaultError, message, default(Exception)); | ||
} | ||
|
||
public void Reject(string code, Exception e) | ||
{ | ||
Reject(code, e.Message, e); | ||
} | ||
|
||
public void Reject(Exception e) | ||
{ | ||
if (e == null) | ||
throw new ArgumentNullException(nameof(e)); | ||
|
||
Reject(DefaultError, e.Message, e); | ||
} | ||
|
||
public void Reject(string code, string message, Exception e) | ||
{ | ||
if (_reject != null) | ||
{ | ||
var errorData = e?.Data; | ||
var userInfo = errorData != null | ||
? JToken.FromObject(errorData) | ||
: null; | ||
_reject.Invoke(new JObject | ||
{ | ||
{ "code", code ?? DefaultError }, | ||
{ "message", message }, | ||
{ "stack", e?.StackTrace }, | ||
{ "userInfo", userInfo }, | ||
}); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; | ||
using Newtonsoft.Json.Linq; | ||
using ReactNative.Bridge; | ||
using System; | ||
using System.Threading; | ||
|
||
namespace ReactNative.Tests.Bridge | ||
{ | ||
[TestClass] | ||
public class PromiseTests | ||
{ | ||
[TestMethod] | ||
public void Promise_Resolve() | ||
{ | ||
var args = default(object[]); | ||
var are = new AutoResetEvent(false); | ||
var resolve = new MockCallback(a => | ||
{ | ||
args = a; | ||
are.Set(); | ||
}); | ||
var reject = new MockCallback(_ => { }); | ||
var promise = new Promise(resolve, reject); | ||
|
||
var o = new object(); | ||
promise.Resolve(o); | ||
are.WaitOne(); | ||
|
||
Assert.IsNotNull(args); | ||
Assert.AreEqual(1, args.Length); | ||
Assert.AreSame(o, args[0]); | ||
} | ||
|
||
[TestMethod] | ||
public void Promise_Reject() | ||
{ | ||
var args = default(object[]); | ||
var are = new AutoResetEvent(false); | ||
var resolve = new MockCallback(_ => { }); | ||
var reject = new MockCallback(a => | ||
{ | ||
args = a; | ||
are.Set(); | ||
}); | ||
var promise = new Promise(resolve, reject); | ||
|
||
var code = "42"; | ||
var message = "foo"; | ||
promise.Reject(code, message); | ||
are.WaitOne(); | ||
Assert.IsNotNull(args); | ||
Assert.AreEqual(1, args.Length); | ||
|
||
var json = args[0] as JObject; | ||
Assert.IsNotNull(json); | ||
Assert.AreEqual(code, json["code"]); | ||
Assert.AreEqual(message, json["message"]); | ||
} | ||
|
||
[TestMethod] | ||
public void Promise_Reject_UserInfo() | ||
{ | ||
var args = default(object[]); | ||
var are = new AutoResetEvent(false); | ||
var resolve = new MockCallback(_ => { }); | ||
var reject = new MockCallback(a => | ||
{ | ||
args = a; | ||
are.Set(); | ||
}); | ||
var promise = new Promise(resolve, reject); | ||
|
||
var code = "42"; | ||
var message = "foo"; | ||
var e = new Exception(); | ||
e.Data.Add("qux", "baz"); | ||
promise.Reject(code, message, e); | ||
are.WaitOne(); | ||
|
||
Assert.IsNotNull(args); | ||
Assert.AreEqual(1, args.Length); | ||
var json = args[0] as JObject; | ||
Assert.IsNotNull(json); | ||
var userInfo = json["userInfo"] as JObject; | ||
Assert.IsNotNull(userInfo); | ||
Assert.AreEqual("baz", userInfo["qux"]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters