本文介绍了Appscript处理的错误未显示在stackdriver错误中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Appscript编写Gmail插件.如果应用程序抛出未处理的错误,它将自动登录到stackdriver上并显示在Stackdriver Error Reporting中.但是,我想手动将错误发送到Stackdriver Error Reporting:

I'm writing a Gmail Addon with Appscript. If the application throws an unhandled error, it will be automatically logged on stackdriver and displayed in Stackdriver Error Reporting. However, I want to manually send errors to Stackdriver Error Reporting:

try{
    thisMayThrowAnError();
catch(err){
    console.error(new Error("Something Failed"))
}

在上面的示例中,错误日志显示在Stackdriver Logs中,但不会出现在Stackdriver Error Reporting中.有没有一种方法可以实现而无需实际抛出(并使加载项崩溃)?

In the example above, the error log is being displayed in Stackdriver Logs, but it won't appear on Stackdriver Error Reporting. Is there a way to achieve this without actually throwing it (and crashing the addon)?

推荐答案

  • 您要手动将自定义错误报告给StackDriver错误报告.
  • 您不想停止脚本.
  • 如果我的理解是正确的,该解决方法如何?在这种解决方法中,它使用Stackdriver Error Reporting API中的projects.events.report方法.

    If my understanding is correct, how about this workaround? In this workaround, it uses the method of projects.events.report in Stackdriver Error Reporting API.

    在使用此示例脚本之前,请执行以下操作.

    Before you use this sample script, please do the following operations.

    • 使用清单和其他将https://www.googleapis.com/auth/cloud-platform添加到范围.
    • 在API控制台上启用Stackdriver Error Reporting API.
    • Add https://www.googleapis.com/auth/cloud-platform to the scopes using Manifests and others.
    • Enable Stackdriver Error Reporting API at API console.
    try {
        thisMayThrowAnError();
    } catch(err) {
      var projectId = "project-id-#####"; // Please set the project ID of project including your script.
      var payload = { // Please modify this for your situation.
        message: "Something Failed",
        serviceContext: {service: "sample"},
        context: {reportLocation: {functionName: "sampleFunctionName"}},
      };
    
      var url = "https://clouderrorreporting.googleapis.com/v1beta1/projects/" + projectId + "/events:report";
      var params = {
        method: "post",
        payload: JSON.stringify(payload),
        contentType: "application/json",
        headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
      }
      UrlFetchApp.fetch(url, params);
    }
    

    注意:

    • 这是一个简单的示例脚本.因此,请根据您的情况进行修改.
    • 在此脚本中,payload的每个值都是样本值.因此,请根据您的情况进行修改.
    • 当我调用此API时,有时会出现结果反射较晚的情况.因此,如果在调用API后结果未反映到StackDriver错误报告中,请稍候.
    • Note:

      • This is a simple sample script. So please modify to your situation.
      • In this script, each value of payload is sample values. So please modify to your situation.
      • When I called this API, there was the case that the reflection of result is late. So if the result is not reflected to StackDriver Error Reporting after you called the API, please wait a moment.
        • Stackdriver Error Reporting API
        • projects.events.report
        • ErrorContext
        • ServiceContext
        • Identifying projects

        根据您的问题,我了解到您正在使用Google Apps脚本.如果您使用其他语言,请告诉我.如果不是您想要的解决方法,我对此表示歉意.

        From your question, I understood that you are using Google Apps Script. If you are using other language, please tell me. If this workaround was not the result you want, I apologize.

        这篇关于Appscript处理的错误未显示在stackdriver错误中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-22 14:01