记录活动和错误

本指南介绍了如何编写自定义日志和错误消息,以帮助排查在“流程”活动标签页中无法运行的流程步骤。

默认情况下,“Activity”标签页会记录在清单文件中定义的正在运行的步的名称。为了帮助您了解在执行某一步骤期间发生了什么,您还应该为该步骤编写自定义日志。如果用户在运行您的步骤时遇到意外行为,日志可以帮助他们了解发生了什么情况。

有用的日志条目具有两个属性:

  • 包含指向相应步骤创建或更新的资源的超链接的条状标签。例如,如果您的步骤创建了 Google 文档,请使用功能块链接到创建的 Google 文档。
  • 详细的错误消息,说明了某步未能运行的原因以及如何解决相应问题。

以下代码示例展示了步骤的 onExecuteFunctionCreateDocument() 如何将成功运行和错误记录到“活动”标签页:

Apps 脚本

// A helper method to return host app actions
function returnActionHelper(action) {
  let hostAppAction = AddOnsResponseService.newHostAppAction()
    .setWorkflowAction(action);

  let renderAction = AddOnsResponseService.newRenderActionBuilder()
    .setHostAppAction(hostAppAction)
    .build();

  return renderAction;
}

function createDocument() {
  let randomInt = Math.floor(Math.random() * 2)
  console.log("The random generated integer is: ", randomInt);
  if (randomInt == 0) {
    console.log("Mock document creation failed.");
    return false;
  } else if (randomInt == 1) {
    console.log("Mock document creation succeeded.");
    return true;
  }
}

function onExecuteFunctionCreateDocument(e) {

  // true if the document is successfully created, false if something goes wrong.
  var successfulRun = createDocument();

  // If successful, return an activity log linking to the created document.
  if (successfulRun == true) {
    let logChip = AddOnsResponseService.newTextFormatChip()
      .setTextFormatIcon(
        AddOnsResponseService.newTextFormatIcon()
          .setMaterialIconName("edit_document")
      )
      .setUrl("https://docs.google.com/document/d/{DOCUMENT}")
      .setLabel("{NAMEOFDOCUMENT}");

    const workflowAction = AddOnsResponseService.newReturnOutputVariablesAction()
      // Set the user-facing error log
      .setLog(
        AddOnsResponseService.newWorkflowTextFormat()
          .addTextFormatElement(
            AddOnsResponseService.newTextFormatElement()
              .setText("Created Google Doc")
          )
          .addTextFormatElement(
            AddOnsResponseService.newTextFormatElement()
              .setTextFormatChip(logChip)
          )
          .addTextFormatElement(
            AddOnsResponseService.newTextFormatElement()
              .setText("Created doc detailing how to improve product.")
          )
      );

    returnActionHelper(workflowAction);
  }
  // Otherwise, return an activity log containing an error explaining what happened and how to resolve the issue.
  else {
    let errorChip = AddOnsResponseService.newTextFormatChip()
      .setTextFormatIcon(
        AddOnsResponseService.newTextFormatIcon()
          .setMaterialIconName("document")
      )
      .setLabel("{NAMEOFDOCUMENT}");

    const workflowAction = AddOnsResponseService.newReturnElementErrorAction()
      .setErrorActionability(AddOnsResponseService.ErrorActionability.NOT_ACTIONABLE)
      .setErrorRetryability(AddOnsResponseService.ErrorRetryability.NOT_RETRYABLE)
      // Set the user-facing error log
      .setErrorLog(
        AddOnsResponseService.newWorkflowTextFormat()
          .addTextFormatElement(
            AddOnsResponseService.newTextFormatElement()
              .setText("Failed to create Google Doc.")
          )
          .addTextFormatElement(
            AddOnsResponseService.newTextFormatElement()
              .setTextFormatChip(errorChip)
          )
          .addTextFormatElement(
            AddOnsResponseService.newTextFormatElement()
              .setText("Unable to create Google Document because OAuth verification failed. Grant one of these authorization scopes and try again: https://www.googleapis.com/auth/documents, https://www.googleapis.com/auth/drive, https://www.googleapis.com/auth/drive.file")
          )
      );

    returnActionHelper(workflowAction);
  }
}