【问题标题】:How to retry failed Vue Concurrency tasks?如何重试失败的 Vue 并发任务?
【发布时间】:2022-06-30 06:00:39
【问题描述】:

我在我的 Vue 3 / TypeScript 项目中使用 Vue Concurrency

我想创建一个任务,如果抛出特定类型的错误,它将重试x 次。

但我不知道如何根据错误消息重试调用。如果错误是INVALID_OAUTH,我想重试几次。否则像平常一样抛出错误。

如何做到这一点?

例如:

const getZohoDeskTicketByIdTask = useTask(function* (signal, ticketId: string) {
  const ticket: string = yield getZohoDeskTicketById({
    ticketId: ticketId,
  });
  // If the above yield returns an error of 'INVALID_OAUTH' it should retry X number of times before failing. If the error is anything else, throw it like normal.
  return ticket;
});

const ticket = await getZohoDeskTicketByIdTask.perform('12345');

【问题讨论】:

    标签: typescript vue.js


    【解决方案1】:

    这可以在任务实例内部处理(因此一个任务实例会执行多个请求,直到它以成功或错误状态完成),也可以在更高级别进行管理,您可以重试整个任务执行操作。

    我实际上很难在这里找到不同之处,也许task.performCount 在某些方面可能有用,但由于您还想检查特定错误而不是所有任务实例都出错,所以这很复杂。

    也许我仍然只是创建一个通用实用程序,既可以用于任务,也可以用于任务之外。

    而且它似乎已经存在: https://github.com/lifeomic/attempt

    try {
      const result = await retry(async (context) => {
        try {
        await getZohoDeskTicketByIdTask.perform('12345');
        } catch (e) {
         if (e.message === 'INVALID_OAUTH') {
           throw e; // rethrow error
         } else {
           // do something else with other errors... maybe also rethrow or cancel task completely with task.cancelAll() ?
         }
        }
      }, options);
    } catch (err) {
    // handle case when retrying failed
    }
    

    我没有测试这段代码,但我会从这样的东西开始。希望有用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      相关资源
      最近更新 更多