* Gets the stack trace of the current call * @param {Error} error * @returns
(error, skip = false)
| 600 | * @returns |
| 601 | */ |
| 602 | function getStack(error, skip = false) { |
| 603 | if (error === null) { |
| 604 | error = new Error(); |
| 605 | } |
| 606 | let stack = error.stack.split("\n"); |
| 607 | if (!skip) stack.splice(1, 1); |
| 608 | let regExecRes = /<([^>]*)>:(\d+):(\d+)/.exec(stack[1]) || []; |
| 609 | if (!regExecRes.length) { |
| 610 | const errorInfo = stack[1]?.split("/").pop(); |
| 611 | regExecRes = /(.+?):(\d+):(\d+)/.exec(errorInfo) || []; |
| 612 | } |
| 613 | let src = ""; |
| 614 | const location = regExecRes[1]; |
| 615 | const lineno = regExecRes[2]; |
| 616 | const colno = regExecRes[3]; |
| 617 | |
| 618 | if (location && lineno) { |
| 619 | src = escapeHTML(`${location} ${lineno}${colno ? ":" + colno : ""}`); |
| 620 | } else { |
| 621 | const res = /\((.*)\)/.exec(stack[1]); |
| 622 | src = res && res[1] ? res[1] : ""; |
| 623 | } |
| 624 | const index = src.indexOf(")"); |
| 625 | src = src |
| 626 | .split("/") |
| 627 | .pop() |
| 628 | .substring(0, index < 0 ? undefined : index); |
| 629 | if (src.length > 50) src = "..." + src.substring(src.length - 50); |
| 630 | |
| 631 | return { |
| 632 | location: src, |
| 633 | stack: stack.join("\n"), |
| 634 | }; |
| 635 | } |
| 636 | |
| 637 | function execute(code) { |
| 638 | let res = null; |