| 87 | } |
| 88 | |
| 89 | function dehydratePromise( |
| 90 | query: Query, |
| 91 | serializeData?: TransformerFn, |
| 92 | shouldRedactErrors?: (error: unknown) => boolean, |
| 93 | ): Promise<unknown> | undefined { |
| 94 | const promise = query.promise?.then(serializeData).catch((error) => { |
| 95 | if (shouldRedactErrors?.(error) === false) { |
| 96 | // Reject original error if it should not be redacted |
| 97 | return Promise.reject(error) |
| 98 | } |
| 99 | // If not in production, log original error before rejecting redacted error |
| 100 | if (process.env.NODE_ENV !== 'production') { |
| 101 | console.error( |
| 102 | `A query that was dehydrated as pending ended up rejecting. [${query.queryHash}]: ${error}; The error will be redacted in production builds`, |
| 103 | ) |
| 104 | } |
| 105 | return Promise.reject(new Error('redacted')) |
| 106 | }) |
| 107 | |
| 108 | // Avoid unhandled promise rejections |
| 109 | // We need the promise we dehydrate to reject to get the correct result into |
| 110 | // the query cache, but we also want to avoid unhandled promise rejections |
| 111 | // in whatever environment the prefetches are happening in. |
| 112 | promise?.catch(noop) |
| 113 | |
| 114 | return promise |
| 115 | } |
| 116 | |
| 117 | // Most config is not dehydrated but instead meant to configure again when |
| 118 | // consuming the de/rehydrated data, typically with useQuery on the client. |