⚠️ ORPC Returning Array of Errors When Using TanStack Query Retries #1106
-
|
Hello team 👋, I’m currently using ORPC within a Turborepo setup powered by Next.js, integrated with TanStack Query for client-side data fetching. In my setup, ORPC handlers internally call APIs (through custom API handlers). This structure works perfectly for me, as my project’s backend is large and cannot be modified directly due to organizational bandwidth constraints. So far, ORPC has been a great fit for bridging server and client requests cleanly. However, I’ve encountered one issue: When a query fails and TanStack Query retries (default 3 attempts), after all retry attempts fail, the returned error becomes an array of errors instead of a single ORPC error object. Here’s my main question:
This is currently my only major concern. Apart from this, ORPC has proven to be an excellent solution for managing server-client API interactions efficiently. Thanks in advance for any clarification or suggestions 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Hi @girishcx07! I'm Dosu and I’m helping the orpc team. The array of errors you’re seeing after TanStack Query retries is generated by TanStack Query’s retry mechanism, not by ORPC itself. ORPC is designed to propagate a single error object per failed request—its error handling logic (including custom error classes, interceptors, and serialization) always returns a single error, not an array. TanStack Query, however, aggregates errors from each failed retry attempt and returns them as an array if all attempts fail source. To normalize this in your components, you can extract the last error from the array (which is usually the most relevant), or map the array to a single error object that matches your expected structure. For example: const normalizedError = Array.isArray(error) ? error[error.length - 1] : error;
// Now handle normalizedError as a single ORPC error objectThis will ensure your error handling logic remains consistent. If you want more type safety or custom normalization, you can also use ORPC’s To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Yes, ORPC supports middleware and lifecycle hooks that let you centrally intercept and normalize errors before they reach the client. You can use the
.use()method to add middleware to your handlers, and inside the middleware, catch errors thrown by downstream handlers or other middleware, then transform or normalize them as needed—such as converting unexpected errors into a consistent ORPC error object or mapping custom errors to your preferred format. This pattern is documented and widely used for centralized error handling in ORPC (docs, middleware docs, discussion).For example: