Skip to content

bug: TASK_MIDDLEWARE_ERROR does not trigger retries despite retry config being set #3231

@isshaddad

Description

@isshaddad

Provide environment information

n/a

Describe the bug

When a task's middleware throws an error, the run fails immediately without retrying, even when a retry config is set on the task. The error is classified as TASK_MIDDLEWARE_ERROR internally, which is marked as retryable in shouldRetryError, but shouldLookupRetrySettings returns false for this error code, causing the retry flow to fall through to fail_run

Expected: a middleware error should retry using the task's retry config just like a run error would.

Reproduction repo

n/a

To reproduce

  1. Add the task below.
  2. Trigger it once.
  3. Run fails on attempt 1 with TASK_MIDDLEWARE_ERROR and does not retry.
import { logger, task } from "@trigger.dev/sdk";

export const middlewareRetryTask = task({
  id: "middleware-retry-task",
  retry: {
    maxAttempts: 3,
    minTimeoutInMs: 500,
    maxTimeoutInMs: 1000,
    randomize: false,
  },
  middleware: async ({ ctx, next }) => {
    logger.info("Middleware running", { attempt: ctx.attempt.number });
    throw new Error("Simulated connection error in middleware");
    await next();
  },
  run: async (payload, { ctx }) => {
    return { attempt: ctx.attempt.number };
  },
});

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions