* Convert isolated-vm error info to a format compatible with the route's error handling
(errorInfo, userCode)
| 108 | * Convert isolated-vm error info to a format compatible with the route's error handling |
| 109 | */ |
| 110 | function convertToCompatibleError(errorInfo, userCode) { |
| 111 | const { name } = errorInfo |
| 112 | let { message, stack } = errorInfo |
| 113 | |
| 114 | message = message |
| 115 | .replace(/\s*\[user-function\.js:\d+:\d+\]/g, '') |
| 116 | .replace(/\s*\[<isolated-vm>:\d+:\d+\]/g, '') |
| 117 | .replace(/\s*\(<isolated-vm>:\d+:\d+\)/g, '') |
| 118 | .trim() |
| 119 | |
| 120 | const lineInfo = extractLineInfo(errorInfo.message, stack) |
| 121 | |
| 122 | let userLine |
| 123 | let lineContent |
| 124 | |
| 125 | if (lineInfo.line !== undefined) { |
| 126 | userLine = lineInfo.line - USER_CODE_START_LINE |
| 127 | const codeLines = userCode.split('\n') |
| 128 | if (userLine > 0 && userLine <= codeLines.length) { |
| 129 | lineContent = codeLines[userLine - 1]?.trim() |
| 130 | } else if (userLine <= 0) { |
| 131 | userLine = 1 |
| 132 | lineContent = codeLines[0]?.trim() |
| 133 | } else { |
| 134 | userLine = codeLines.length |
| 135 | lineContent = codeLines[codeLines.length - 1]?.trim() |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (stack) { |
| 140 | stack = stack.replace(/<isolated-vm>:(\d+):(\d+)/g, (_, line, col) => { |
| 141 | const adjustedLine = Number.parseInt(line, 10) - USER_CODE_START_LINE |
| 142 | return `user-function.js:${Math.max(1, adjustedLine)}:${col}` |
| 143 | }) |
| 144 | stack = stack.replace(/at <isolated-vm>:(\d+):(\d+)/g, (_, line, col) => { |
| 145 | const adjustedLine = Number.parseInt(line, 10) - USER_CODE_START_LINE |
| 146 | return `at user-function.js:${Math.max(1, adjustedLine)}:${col}` |
| 147 | }) |
| 148 | } |
| 149 | |
| 150 | return { |
| 151 | message, |
| 152 | name, |
| 153 | stack, |
| 154 | line: userLine, |
| 155 | column: lineInfo.column, |
| 156 | lineContent, |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Execute code in isolated-vm |
no test coverage detected