【问题标题】:Catching Errors with Google Classroom API使用 Google Classroom API 捕捉错误
【发布时间】:2017-09-11 05:16:50
【问题描述】:

使用 Google Classroom API 捕获错误的正确方法是什么?

我正在尝试做类似的事情:

gapi.client.classroom.courses.list({
  pageSize: 100
}).then((response) => {
  return process(response);
});

但如果没有 Classroom 帐户的 Google 用户执行此代码,则会引发错误,我不清楚如何捕获/处理它。

我尝试用try {...} catch(error) {...} 包装这段代码,但没有成功。我还尝试在 .then() 声明之后添加 .catch(function(error){ ... });,但这似乎也不起作用。

【问题讨论】:

    标签: google-classroom


    【解决方案1】:

    答案在这里: https://developers.google.com/api-client-library/javascript/features/promises

    .then 调用有两个参数。第一个是成功时调用的函数,第二个是失败时调用的函数:

    .then(
      function(response) {
        return process(response);
      },
      function(errorResponse) {
        return handleError(errorResponse);
      }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 2014-04-18
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 2021-05-09
      相关资源
      最近更新 更多