Skip to content

feat: Retryable errors #4789

Closed as not planned
Closed as not planned

Description

@reconbot

Describe the feature you'd like to request

Building on #1714 and the AWS SDK we have a list of status codes and error types that are retryable. (Personally I threw a TRPCError({ code: 'NOT_FOUND' }) error and was surprised to see the query retry 3 times over 7 seconds per react queries defaults.)

In the linked issue it was mentioned not wanting to have retry behavior in trpc, but we do have the 3 by default. Retries on all errors gives a bad experience and makes apis that use errors seem 7 seconds slower than they need to be because of the default exponential backoff.

I'd like to make this better by default and easily extendable.

Describe the solution you'd like to see

If we take the list of http codes from AWS and apply them to the TRPC Error codes we can export and use by default a reasonable retry behavior.

So from codes.ts And from the aws docs

const retry = (failureCount:number, error: unknown) => {
  if (!(error instanceof TRPCClientError)) {
    return false
  }

  const retryableCodes = new Set([
    'BAD_REQUEST',
    'TIMEOUT',
    'INTERNAL_SERVER_ERROR',
    'TOO_MANY_REQUESTS',
  ])

  if (!retryableCodes.has(error.data.code)) {
    return false
  }

  return failureCount < 3
}

This would be used when creating the client trpc object. For nextjs it would look like;

export const trpc = createTRPCNext<AppRouter>({
  config(opts) {
    return {
      queryClientConfig: {
        defaultOptions: {
          queries: {
            retry,
          },
        },
      },
    }
  },
})

If someone wants something different they can import the function and handle their errors before calling the default one.

A similar approach would be the AWS-SDK's method of adding a retryable boolean to the Error object.

Describe alternate solutions

Disable all retries by default. I'd argue that adding an accidental 7 seconds of latency to a query is worse than not retrying.

Additional information

I know trpc is a thing layer over react-query and this changes the default behavior of react-query, however since our examples show code throwing errors vs returning undefined or an error object I think we should support it out of the box.

As an aside, I come form a long history of graphql and react applications and TRPC is a fantastic replacement. I want to thank you for such a fundamental shift in how we build these apis. You've removed a ton of complexity and overhead.

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR implementing this feature!

Activity

  1. KATT commented on Sep 12, 2023

    @KATT
    Member

    The way you did it here is exactly how I've implemented it in the past.

    I don't want us to build any features around this nor change default behaviors, but docs would be good. Feel free to reopen this issue with a docs change request or just have a stab at updating the docs at an appropriate place.

  2. reconbot commented on Sep 12, 2023

    @reconbot
    Author

    Happy to, how should I explain the default behavior?

  3. locked as resolved and limited conversation to collaborators on Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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