如何在 Google Apps Script Webapp 上同时处理错误和成功
How to handle errors and success at the same time on a Google Apps Script Webapp
我想知道与 Google Apps Script Webapps 的客户端-服务器通信是如何工作的。在 html 页面中,我用
调用我的函数
google.script.run.doSomething();
我可以添加一个
withFailureHandler(onFailure);
或
withSuccessHandler(onSuccess)
但我不能同时添加..
所以当我想调用服务器端函数时,我通常想处理 UI 中的响应?成功与失败的方式不同,对吧?但我想同时处理这两者,对吗?那么为什么我必须在其中选择一个呢?
还有一个问题是我找不到任何关于您在服务器端可能遇到的实际错误的信息,因此您的 withFailureHandler(onFailure);
可以处理它们。我可以做一个 throw new Error("everything is broken - tell that the user")
吗?如果有权限错误,是否处理?如何在服务器端生成错误以便我可以在客户端正确处理它?
documentation确实没有明确说明,但是你可以同时实现成功和错误处理程序
样本:
<html>
<head>
<base target="_top">
<script>
google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).getUnreadEmails();
function onSuccess(numUnread) {
var div = document.getElementById('output');
div.innerHTML = 'You have ' + numUnread + ' unread messages in your Gmail inbox.';
}
function onFailure(error) {
var div = document.getElementById('output');
div.innerHTML = "ERROR: " + error.message;
}
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>
更新
要根据文档示例模拟故障,请更改工作 code.gs
部分
来自
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function getUnreadEmails() {
return GmailApp.gotInboxUnreadCount();
}
到
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function getUnreadEmails() {
return GMailApp.gotInboxUnreadCount();
}
部署WebApp后,html会输出:
中所写
You can use any combination and any order of withSuccessHandler(), withFailureHandler(), and withUserObject(). You can also call any of the modifying functions on a script runner that already has a value set. The new value simply overrides the previous value.
样本:
const gsr = google.script.run;
const runnerF = gsr.withFailureHandler(onFailure);
const runnerS1 = runnerF.withSuccessHandler(onSuccess1);
const runnerS2 = runnerF.withSuccessHandler(onSuccess2);
/*Both runners have the same failure handlers, but different success handlers*/
runnerS1.callServer();//failure => onFailure;success => onSuccess;
runnerS2.callServer2();//failure => onFailure;success => onSuccess2;
我想知道与 Google Apps Script Webapps 的客户端-服务器通信是如何工作的。在 html 页面中,我用
调用我的函数google.script.run.doSomething();
我可以添加一个
withFailureHandler(onFailure);
或
withSuccessHandler(onSuccess)
但我不能同时添加..
所以当我想调用服务器端函数时,我通常想处理 UI 中的响应?成功与失败的方式不同,对吧?但我想同时处理这两者,对吗?那么为什么我必须在其中选择一个呢?
还有一个问题是我找不到任何关于您在服务器端可能遇到的实际错误的信息,因此您的 withFailureHandler(onFailure);
可以处理它们。我可以做一个 throw new Error("everything is broken - tell that the user")
吗?如果有权限错误,是否处理?如何在服务器端生成错误以便我可以在客户端正确处理它?
documentation确实没有明确说明,但是你可以同时实现成功和错误处理程序
样本:
<html>
<head>
<base target="_top">
<script>
google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).getUnreadEmails();
function onSuccess(numUnread) {
var div = document.getElementById('output');
div.innerHTML = 'You have ' + numUnread + ' unread messages in your Gmail inbox.';
}
function onFailure(error) {
var div = document.getElementById('output');
div.innerHTML = "ERROR: " + error.message;
}
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>
更新
要根据文档示例模拟故障,请更改工作 code.gs
部分
来自
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function getUnreadEmails() {
return GmailApp.gotInboxUnreadCount();
}
到
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function getUnreadEmails() {
return GMailApp.gotInboxUnreadCount();
}
部署WebApp后,html会输出:
You can use any combination and any order of withSuccessHandler(), withFailureHandler(), and withUserObject(). You can also call any of the modifying functions on a script runner that already has a value set. The new value simply overrides the previous value.
样本:
const gsr = google.script.run;
const runnerF = gsr.withFailureHandler(onFailure);
const runnerS1 = runnerF.withSuccessHandler(onSuccess1);
const runnerS2 = runnerF.withSuccessHandler(onSuccess2);
/*Both runners have the same failure handlers, but different success handlers*/
runnerS1.callServer();//failure => onFailure;success => onSuccess;
runnerS2.callServer2();//failure => onFailure;success => onSuccess2;