Javascript Google Apps 脚本中未处理的异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44913717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unhandled exceptions in Google Apps Script
提问by tenbits
I have created a public Web App with access to my private spreadsheet data. I can catch and log exceptions intry..catch, but:
我创建了一个公共 Web 应用程序,可以访问我的私人电子表格数据。我可以在 中捕获和记录异常try..catch,但是:
- is it possible to catch all unhandled exceptions, like browsers
window.onerror? - can I view logs of unhandled exceptions somewhere?
- by exceptions like "Service invoked too many times" my app is even not getting run, so here I definitely can`t handle the exceptions. Is there logs with such kind of exceptions?
- 是否可以捕获所有未处理的异常,例如浏览器
window.onerror? - 我可以在某处查看未处理异常的日志吗?
- 由于诸如“服务调用次数过多”之类的异常,我的应用程序甚至无法运行,因此在这里我绝对无法处理这些异常。有没有这种异常的日志?
These are so simple questions, so that I'm bit confused to ask them, but after hours of research I could not find the answers.
这些问题太简单了,所以我问他们有点困惑,但经过几个小时的研究,我找不到答案。
Thank you in advance.
先感谢您。
采纳答案by Spencer Easton
These are issues that are being addressed currently. Right now in the Apps Script Early Access Program are two new additions that handle these cases. The first is native integration with stackdriver logging and the addition of google.script.run.withLogger().
这些都是目前正在解决的问题。目前,Apps Script Early Access Program 中有两个新增功能可以处理这些情况。第一个是与 Stackdriver 日志记录的本地集成以及google.script.run.withLogger().
First off for now you need to apply for the EAP:
首先,您现在需要申请 EAP:
https://developers.google.com/apps-script/guides/apps-script-eap
https://developers.google.com/apps-script/guides/apps-script-eap
Stackdriver Logging:
Stackdriver 日志记录:
To log to stackdriver the consoleobject has been added to the server side.
要登录到 stackdriver,该console对象已添加到服务器端。
code.gs
代码.gs
console.log('This will log to stackdriver')
Check out the docs for all the methods of console.
查看文档以了解console.
https://developers.google.com/apps-script/guides/logging#stackdriver_logging
https://developers.google.com/apps-script/guides/logging#stackdriver_logging
Example from the docs:
文档中的示例:
function measuringExecutionTime() {
// A simple INFO log message, using sprintf() formatting.
console.info('Timing the %s function (%d arguments)', 'myFunction', 1);
// Log a JSON object at a DEBUG level. The log is labeled
// with the message string in the log viewer, and the JSON content
// is displayed in the expanded log structure under "structPayload".
var parameters = {
isValid: true,
content: 'some string',
timestamp: new Date()
};
console.log({message: 'Function Input', initialData: parameters});
var label = 'myFunction() time'; // Labels the timing log entry.
console.time(label); // Starts the timer.
try {
myFunction(parameters); // Function to time.
} catch (e) {
// Logs an ERROR message.
console.error('myFunction() yielded an error: ' + e);
}
console.timeEnd(label);
}
In addition you can also check Log Exceptionsin the scripts properties. This will generate a stackdriver entry every time any error occurs in your script.
此外,您还可以检查Log Exceptions脚本属性。每次脚本中发生任何错误时,这都会生成一个堆栈驱动程序条目。
Error recovery in a web app
Web 应用程序中的错误恢复
To recover in a web app from a failure you have access to the withFailureHandler()method found in the google.script.runobject. With this you can register a callback in the event your script hits an exception.
要在 Web 应用程序中从故障中恢复,您可以访问withFailureHandler()在google.script.run对象中找到的方法。有了这个,您可以在脚本遇到异常时注册回调。
Full documentation can be found at:
可以在以下位置找到完整的文档:
https://developers.google.com/apps-script/guides/html/reference/run
https://developers.google.com/apps-script/guides/html/reference/run
If you are doing server side checks with try...catchyou may be getting an exception but gracefully handling it. In this case withFailureHandler() will not execute and onSuccessHandler() propably isnt the best place to handle errors. In the EAP there is now a withLoggermethod to google.script.run. For now there no documentation for google.script.run.withLogger(). I found it by digging through devtools. withLogger()allows you to register a function as a callback when ever a stackdriver entry is created. This is particularly helpful when you have log exceptionschecked in your script properties. In this sense it is a bit like withFailureHandler()but it can be triggered by any stackdriver entry you add though the server-side consoleobject.
如果您正在执行服务器端检查,try...catch您可能会收到异常但会妥善处理它。在这种情况下 withFailureHandler() 将不会执行并且 onSuccessHandler() 可能不是处理错误的最佳位置。在 EAP 中,现在有一种withLogger方法可以google.script.run. 目前没有关于google.script.run.withLogger(). 我通过挖掘开发工具找到了它。withLogger()允许您在创建 stackdriver 条目时将函数注册为回调。这在您log exceptions签入脚本属性时特别有用。从这个意义上说,它有点像withFailureHandler()但它可以由您通过服务器端console对象添加的任何堆栈驱动程序条目触发。
index.html
索引.html
<script>
google.script.run
.withSuccessHandler(function(){console.log('OK')})
.withFailureHandler(function(e){console.error(e)})
.withLogger(function(e){console.warn("The following log was generated:"+e)})
.serverFunctionCall();
</script>
code.gs
代码.gs
function serverFunctionCall(){
console.log("This log will generate a callback");
return true;
}

