Skip to content

Commit 17d85d0

Browse files
authored
Throw on JS exceptions instead of returning default values (#4)
1 parent ae271b5 commit 17d85d0

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

Assets/Thirdweb/Scripts/Bridge.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,9 @@ public static async Task<T> InvokeRoute<T>(string route, string[] body)
9090
var task = new TaskCompletionSource<string>();
9191
taskMap[taskId] = task;
9292
ThirdwebInvoke(taskId, route, msg, jsCallback);
93-
try
94-
{
95-
string result = await task.Task;
96-
// Debug.LogFormat("Result from {0}: {1}", route, result);
97-
return JsonConvert.DeserializeObject<Result<T>>(result).result;
98-
}
99-
catch (Exception)
100-
{
101-
// Debug.LogFormat("Error from {0}: {1}", route, e);
102-
return default(T);
103-
}
93+
string result = await task.Task;
94+
Debug.Log($"InvokeRoute Result: {result}");
95+
return JsonConvert.DeserializeObject<Result<T>>(result).result;
10496
}
10597

10698
[DllImport("__Internal")]

Assets/WebGLTemplates/Thirdweb/lib/thirdweb.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,21 @@ w.bridge.connect = async () => {
4343
});
4444
return await w.thirdweb.wallet.getAddress();
4545
} else {
46-
console.error("window.thirdweb is not defined");
47-
return null;
46+
throw "window.thirdweb is not defined";
4847
}
4948
} else {
50-
console.error("Please install a wallet browser extension");
51-
return null;
49+
throw "Please install a wallet browser extension";
5250
}
5351
};
5452

5553
w.bridge.switchNetwork = async (chainId) => {
56-
try {
57-
if (chainId) {
58-
await window.ethereum.request({
59-
method: "wallet_switchEthereumChain",
60-
params: [{ chainId: "0x" + chainId.toString(16) }],
61-
});
62-
} else {
63-
console.error("Error switrching network");
64-
return null;
65-
}
66-
} catch (e) {
67-
console.error("Error switrching network", e);
68-
return null;
54+
if (chainId) {
55+
await window.ethereum.request({
56+
method: "wallet_switchEthereumChain",
57+
params: [{ chainId: "0x" + chainId.toString(16) }],
58+
});
59+
} else {
60+
throw "Error Switching Network";
6961
}
7062
};
7163

@@ -94,8 +86,7 @@ w.bridge.invoke = async (route, payload) => {
9486
const result = await w.thirdweb[prop][routeArgs[1]](...parsedArgs);
9587
return JSON.stringify({ result: result }, bigNumberReplacer);
9688
} else {
97-
console.error("invalid route", route);
98-
return null;
89+
throw "Invalid Route";
9990
}
10091
}
10192

@@ -122,8 +113,7 @@ w.bridge.invoke = async (route, payload) => {
122113
);
123114
return JSON.stringify({ result: result }, bigNumberReplacer);
124115
} else {
125-
console.error("invalid route", route);
126-
return null;
116+
throw "Invalid Route";
127117
}
128118
}
129119
};

0 commit comments

Comments
 (0)