(stack: string)
| 13 | * Returns the original stack trace if source maps can't be applied |
| 14 | */ |
| 15 | export async function applySourceMapsToStack(stack: string): Promise<string> { |
| 16 | if (!stack) { |
| 17 | console.debug("applySourceMapsToStack: Empty stack trace provided") |
| 18 | return stack |
| 19 | } |
| 20 | |
| 21 | console.debug("Original stack trace:", stack) |
| 22 | |
| 23 | try { |
| 24 | // Create a temporary Error object with the provided stack |
| 25 | const tempError = new Error() |
| 26 | tempError.stack = stack |
| 27 | |
| 28 | // Extract the error message (first line) |
| 29 | const errorMessage = stack.split("\n")[0] |
| 30 | console.debug("Error message:", errorMessage) |
| 31 | |
| 32 | // Use StackTrace.js to get source mapped stack frames |
| 33 | const stackFrames = await StackTrace.fromError(tempError) |
| 34 | console.debug("StackTrace.js parsed frames:", stackFrames) |
| 35 | |
| 36 | // Convert stack frames back to string format |
| 37 | const mappedFrames = stackFrames.map((frame: StackTrace.StackFrame) => { |
| 38 | const functionName = frame.functionName || "<anonymous>" |
| 39 | const fileName = frame.fileName || "unknown" |
| 40 | const lineNumber = frame.lineNumber || 0 |
| 41 | const columnNumber = frame.columnNumber || 0 |
| 42 | |
| 43 | return ` at ${functionName} (${fileName}:${lineNumber}:${columnNumber})` |
| 44 | }) |
| 45 | |
| 46 | // Reconstruct the stack trace with the error message |
| 47 | const result = [errorMessage, ...mappedFrames].join("\n") |
| 48 | console.debug("Final mapped stack trace:", result) |
| 49 | return result |
| 50 | } catch (error) { |
| 51 | console.error("Error applying source maps with StackTrace.js:", error) |
| 52 | return stack // Return original stack on error |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Apply source maps to a React component stack trace using StackTrace.js |
no test coverage detected