TinyTiger
TinyTiger

Reputation: 2093

How to retry failed Vue Concurrency tasks?

I am using Vue Concurrency in my Vue 3 / TypeScript project.

And I want to create a Task that will retry x number of times if a specific kind of error is thrown.

But I can't figure out how to retry the call based on it's error message. I want to retry the call a limited number of times if the error is INVALID_OAUTH. Otherwise just throw the error like normal.

How can this be done?

For example:

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');

Upvotes: 1

Views: 203

Answers (1)

Martin Malinda
Martin Malinda

Reputation: 1608

This can be either handled inside the task instance (therefore one task instance does multiple requests until it finishes with either success or error state) or it can be managed one level higher and you can retry the whole task perform action.

I'm actually struggling to find the difference here, perhaps task.performCount could be useful in some ways but since you want to also check for specific errors and not all task instances error out, it's complicated.

Maybe I'd still just create a generic utility that could be used both for tasks but also outside of them.

And it seems like it already exists: 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
}

I didn't test this code, but I'd start with something like this. Hope it's useful!

Upvotes: 1

Related Questions