* Prepares the in-flight promise of a pending query for dehydration: applies `serializeData` to its * result, and redacts its rejection error unless `shouldRedactErrors` returns `false` for it. * @param query - The pending query whose promise is dehydrated. * @param serializeData - Optional trans
( query: Query, serializeData?: TransformerFn, shouldRedactErrors?: (error: unknown) => boolean, )
| 137 | * @returns The transformed promise, or `undefined` if the query has no in-flight promise. |
| 138 | */ |
| 139 | function dehydratePromise( |
| 140 | query: Query, |
| 141 | serializeData?: TransformerFn, |
| 142 | shouldRedactErrors?: (error: unknown) => boolean, |
| 143 | ): Promise<unknown> | undefined { |
| 144 | const promise = query.promise?.then(serializeData).catch((error) => { |
| 145 | if (shouldRedactErrors?.(error) === false) { |
| 146 | // Reject original error if it should not be redacted |
| 147 | return Promise.reject(error) |
| 148 | } |
| 149 | // If not in production, log original error before rejecting redacted error |
| 150 | if (process.env.NODE_ENV !== 'production') { |
| 151 | console.error( |
| 152 | `A query that was dehydrated as pending ended up rejecting. [${query.queryHash}]: ${error}; The error will be redacted in production builds`, |
| 153 | ) |
| 154 | } |
| 155 | return Promise.reject(new Error('redacted')) |
| 156 | }) |
| 157 | |
| 158 | // Avoid unhandled promise rejections |
| 159 | // We need the promise we dehydrate to reject to get the correct result into |
| 160 | // the query cache, but we also want to avoid unhandled promise rejections |
| 161 | // in whatever environment the prefetches are happening in. |
| 162 | promise?.catch(noop) |
| 163 | |
| 164 | return promise |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Dehydrates a single `Query` into a serializable `DehydratedQuery` snapshot. Note that most query config (e.g. |