Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better coroutine error handling #54

Merged
merged 7 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Runtime/Scripts/Helpers/CoroutineUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using UnityEngine;
using System.Collections;

public static class CoroutineUtils
{
public static Coroutine TryWith(
MonoBehaviour monoBehaviour,
IEnumerator coroutine,
Action<Exception> exceptionHandler = null)
{
return monoBehaviour.StartCoroutine(Try(coroutine, exceptionHandler));
}

public static IEnumerator Try(
IEnumerator coroutine,
Action<Exception> exceptionHandler = null)
{
while (true)
{
object current;
try
{
if (coroutine.MoveNext() == false)
{
break;
}
current = coroutine.Current;
}
catch (Exception ex)
{
if (exceptionHandler == null)
{
Debug.LogError(ex);
}
else
{
exceptionHandler?.Invoke(ex);
}
yield break;
}
yield return current;
}
exceptionHandler?.Invoke(null);
k-karuna marked this conversation as resolved.
Show resolved Hide resolved
}
}
11 changes: 11 additions & 0 deletions Runtime/Scripts/Helpers/CoroutineUtils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Runtime/Scripts/Helpers/UnityMainThreadDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Enqueue(IEnumerator action)
{
lock (_executionQueue)
{
_executionQueue.Enqueue(() => { StartCoroutine(action); });
_executionQueue.Enqueue(() => { CoroutineUtils.TryWith(this, action); });
}
}

Expand Down
8 changes: 7 additions & 1 deletion Runtime/Scripts/TezosAPI/Tezos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ private void InitBeaconConnector()
{
var json = JsonSerializer.Deserialize<JsonElement>(transaction);
var transactionHash = json.GetProperty("transactionHash").GetString();
MessageReceiver.StartCoroutine(MessageReceiver.ContractCallInjection(_indexerNode, transactionHash));
CoroutineUtils.TryWith(MessageReceiver,
MessageReceiver.ContractCallInjection(_indexerNode, transactionHash),
(ex) =>
{
if (ex != null)
Debug.Log("An exception related to Tezos ContractCallInjection: " + ex);
});
};
}

Expand Down
12 changes: 9 additions & 3 deletions Samples~/Scripts/DemoExample/CopyToClipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ public void OnPointerClick(PointerEventData eventData)
{
if (_blockCopy)
return;

#if UNITY_WEBGL
inputField.gameObject.SetActive(true);
inputField.text = text.text;
text.gameObject.SetActive(false);
#endif

// copy text to the clipboard
GUIUtility.systemCopyBuffer = text.text;
CoroutineRunner.Instance.StartCoroutine(OnTextCopied());
CoroutineUtils.TryWith(CoroutineRunner.Instance,
OnTextCopied(),
(ex) =>
{
if (ex != null)
Debug.Log("An exception related to CopyToClipboard: " + ex);
});
}

IEnumerator OnTextCopied()
Expand Down
34 changes: 18 additions & 16 deletions Samples~/Scripts/DemoExample/Core/ExampleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public void FetchInventoryItems(Action<List<IItemModel>> callback)
var entrypoint = "view_items_of";
var input = new { @string = sender };

CoroutineRunner.Instance.StartCoroutine(
_tezos.ReadView(contractAddress, entrypoint, input, result =>
CoroutineUtils.TryWith(CoroutineRunner.Instance,
_tezos.ReadView(contractAddress, entrypoint, input, result =>
{
Debug.Log("READING INVENTORY DATA");
// deserialize the json data to inventory items
CoroutineRunner.Instance.StartCoroutine(
CoroutineUtils.TryWith(CoroutineRunner.Instance,
BeaconSDK.NetezosExtensions.HumanizeValue(result, _networkRPC, destination, "humanizeInventory",
(ContractInventoryViewResult[] inventory) => OnInventoryFetched(inventory, callback))
);
Expand Down Expand Up @@ -127,14 +127,15 @@ public void FetchMarketItems(Action<List<IItemModel>> callback)
Prim = PrimType.Unit
};

CoroutineRunner.Instance.StartCoroutine(
_tezos.ReadView(contractAddress, entrypoint, input, result =>
CoroutineUtils.TryWith(CoroutineRunner.Instance,
_tezos.ReadView(contractAddress, entrypoint, input, result =>
{
// deserialize the json data to market items
CoroutineRunner.Instance.StartCoroutine(
Debug.Log("READING INVENTORY DATA");
// deserialize the json data to inventory items
CoroutineUtils.TryWith(CoroutineRunner.Instance,
BeaconSDK.NetezosExtensions.HumanizeValue(result, _networkRPC, destination, "humanizeMarketplace",
(ContractMarketplaceViewResult[] market) => OnMarketplaceFetched(market, callback))
);
);
}));
}

Expand Down Expand Up @@ -217,7 +218,7 @@ public User GetCurrentUser()
public void GetBalance(Action<ulong> callback)
{
var routine = _tezos.ReadBalance(callback);
CoroutineRunner.Instance.StartCoroutine(routine);
CoroutineUtils.TryWith(CoroutineRunner.Instance, routine);
}

public void GetSoftBalance(Action<int> callback)
Expand All @@ -239,13 +240,13 @@ private void GetSoftBalanceRoutine(Action<int> callback)
}
};

CoroutineRunner.Instance.StartCoroutine(
CoroutineUtils.TryWith(CoroutineRunner.Instance,
_tezos.ReadView(contractAddress, "get_balance", input, result =>
{
var intProp = result.GetProperty("int");
var intValue = Convert.ToInt32(intProp.ToString());
callback(intValue);
}));
{
var intProp = result.GetProperty("int");
var intValue = Convert.ToInt32(intProp.ToString());
callback(intValue);
}));
}

public void TransferItem(int itemID, int amount, string address)
Expand Down Expand Up @@ -347,7 +348,8 @@ public void IsItemOnMarket(int itemID, string owner, Action<bool> callback)
}
};

CoroutineRunner.Instance.StartCoroutine(

CoroutineUtils.TryWith(CoroutineRunner.Instance,
_tezos.ReadView(contractAddress, entrypoint, input, result =>
{
var boolString = result.GetProperty("prim");
Expand Down
17 changes: 15 additions & 2 deletions Samples~/Scripts/DemoExample/UIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ private void PopulateInventory(List<IItemModel> items)
inventory.Init(items);
loadingPanel.SetActive(false);
};
StartCoroutine(DoActionNextFrame(action));
CoroutineUtils.TryWith(this,
DoActionNextFrame(action),
(ex) =>
{
if (ex != null)
Debug.Log("An exception related to PopulateInventory: " + ex);
});
}

private void PopulateMarket(List<IItemModel> items)
Expand All @@ -108,7 +114,14 @@ private void PopulateMarket(List<IItemModel> items)
market.Init(items);
loadingPanel.SetActive(false);
};
StartCoroutine(DoActionNextFrame(action));

CoroutineUtils.TryWith(this,
DoActionNextFrame(action),
(ex) =>
{
if (ex != null)
Debug.Log("An exception related to PopulateMarket: " + ex);
});
}

private IEnumerator DoActionNextFrame(Action action)
Expand Down