(errorOriginal: unknown, errorInfo?: ErrorInfo)
| 91 | // - Client counterpart: https://github.com/vikejs/vike-react/blob/80fde437bfab666e5b4a0a30386300363bd99939/packages/vike-react/src/integration/onRenderClient.tsx#L108 |
| 92 | type ErrorInfo = { componentStack?: string } |
| 93 | function getErrorWithComponentStack(errorOriginal: unknown, errorInfo?: ErrorInfo) { |
| 94 | if (!errorInfo?.componentStack || !isObject(errorOriginal)) return errorOriginal |
| 95 | const errorStackLines = String(errorOriginal.stack).split('\n') |
| 96 | |
| 97 | // Inject the component stack right before the React stack trace (potentially *after* some vike-react or react-streaming strack trace, e.g. if react-streaming's useAsync() throws an error). |
| 98 | const cutoff = errorStackLines.findIndex((l) => { |
| 99 | l = toPosixPath(l) |
| 100 | return l.includes('node_modules/react-dom/') || l.includes('node_modules/react/') |
| 101 | }) |
| 102 | if (cutoff === -1) return errorOriginal |
| 103 | |
| 104 | const errorStackLinesBegin = errorStackLines.slice(0, cutoff) |
| 105 | const errorStackLinesEnd = errorStackLines.slice(cutoff) |
| 106 | const componentStackLines = errorInfo.componentStack.split('\n').filter(Boolean) |
| 107 | if (componentStackLines[0] === errorStackLinesBegin.at(-1)) componentStackLines.shift() |
| 108 | const stackEnhanced = [ |
| 109 | // |
| 110 | ...errorStackLinesBegin, |
| 111 | ...componentStackLines, |
| 112 | ...errorStackLinesEnd, |
| 113 | ].join('\n') |
| 114 | |
| 115 | const errorBetter = getBetterError(errorOriginal, { stack: stackEnhanced }) |
| 116 | return errorBetter |
| 117 | } |
no test coverage detected
searching dependent graphs…