Google Apps脚本中未处理的例外情况 [英] Unhandled exceptions in Google Apps Script

查看:112
本文介绍了Google Apps脚本中未处理的例外情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个公共Web应用程序,可以访问我的私人电子表格数据。我可以在 try..catch 中捕获并记录异常,但是:

I have created a public Web App with access to my private spreadsheet data. I can catch and log exceptions intry..catch, but:


  1. 是可以捕获所有未处理的异常,例如浏览器 window.onerror

  2. 我可以在某处查看未处理异常的日志吗?

  3. 例如服务调用太多次我的应用甚至没有运行,所以在这里,我绝对不能处理异常。是否有这种例外的日志?

  1. is it possible to catch all unhandled exceptions, like browsers window.onerror?
  2. can I view logs of unhandled exceptions somewhere?
  3. 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?

这些都是如此简单的问题,所以我有点疑惑他们,但经过几个小时的研究,我找不到答案。

提前谢谢你。

推荐答案

这些是目前正在解决的问题。现在,在Apps脚本中,早期访问计划是处理这些案例的两个新增功能。第一个是与stackdriver日志记录的本机集成以及 google.script.run.withLogger()的添加。

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().

首先,您需要申请EAP:

First off for now you need to apply for the EAP:



https://developers.google.com/apps -script / guides / apps-script-eap




Stackdriver Logging:



要登录到stackdriver,控制台对象已添加到服务器端。

Stackdriver Logging:

To log to stackdriver the console object has been added to the server side.

code.gs

console.log('This will log to stackdriver')  

查看 console <的所有方法的文档/ code>。

Check out the docs for all the methods of console.



https://developers.google.com/apps-script/guides/logging#stackdriver_logging


来自文档的示例:

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);     
  }

此外,您还可以检查日志例外。每次脚本中发生任何错误时,都会生成一个stackdriver条目。

In addition you can also check Log Exceptions in the scripts properties. This will generate a stackdriver entry every time any error occurs in your script.

要在网络应用中从故障中恢复,您可以访问 withFailureHandler() google.script.run 对象中找到的方法。有了这个,您可以在脚本遇到异常的情况下注册回调。

To recover in a web app from a failure you have access to the withFailureHandler() method found in the google.script.run object. With this you can register a callback in the event your script hits an exception.

完整文档可在以下网址找到:

Full documentation can be found at:



https://developers.google.com/apps-script/guides/ html / reference / run


如果您使用<$进行服务器端检查c $ c> try ... catch 你可能会得到一个例外但是优雅地处理它。在这种情况下,withFailureHandler()将不会执行,onSuccessHandler()可能不是处理错误的最佳位置。在EAP中,现在有一个 withLogger 方法到 google.script.run 。目前还没有 google.script.run.withLogger()的文档。我通过挖掘devtools找到了它。 withLogger()允许您在创建堆栈驱动程序条目时将函数注册为回调。当您在脚本属性中选中日志异常时,这尤其有用。在这个意义上它有点像 withFailureHandler()但它可以通过服务器端控制台 object。

If you are doing server side checks with try...catch you 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 withLogger method 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 exceptions checked 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 console object.

index.html

index.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

function serverFunctionCall(){
   console.log("This log will generate a callback");
   return true;

}

这篇关于Google Apps脚本中未处理的例外情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆