ثبت فعالیت‌ها و خطاها

این راهنما نحوه نوشتن گزارش‌های سفارشی و پیام‌های خطا را توضیح می‌دهد که به عیب‌یابی مرحله‌ای از جریان که در تب فعالیت جریان‌ها اجرا نمی‌شود، کمک می‌کند.

به طور پیش‌فرض، تب Activity نام مرحله‌ای که اجرا می‌شود را همانطور که در فایل manifest تعریف شده است، ثبت می‌کند. برای اینکه به شما در درک اتفاقات رخ داده در طول اجرای یک مرحله کمک کند، باید گزارش‌های سفارشی نیز برای مرحله خود بنویسید. اگر کاربران هنگام اجرای مرحله شما با رفتار غیرمنتظره‌ای مواجه شوند، گزارش‌های شما می‌توانند به آنها در درک آنچه اتفاق افتاده است کمک کنند.

یک ورودی مفید برای گزارش، دو ویژگی دارد:

  • تراشه‌ای حاوی یک هایپرلینک به منبعی که توسط مرحله ایجاد یا به‌روزرسانی شده است. برای مثال، اگر مرحله شما یک سند گوگل ایجاد می‌کند، از تراشه برای پیوند به سند گوگل ایجاد شده استفاده کنید.
  • یک پیام خطای دقیق که توضیح می‌دهد چرا یک مرحله اجرا نشد و چگونه می‌توان مشکل را حل کرد.

نمونه کد زیر نشان می‌دهد که چگونه تابع onExecuteFunctionCreateDocument() در یک مرحله می‌تواند یک اجرای موفق و یک خطا را در برگه فعالیت ثبت کند:

اسکریپت برنامه‌ها

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