|
| 1 | +import { |
| 2 | + ComponentPublicInstance, |
| 3 | + defineComponent, |
| 4 | + h as vueH, |
| 5 | + VNode, |
| 6 | +} from "vue"; |
| 7 | +import { ErrorsDef, ErrorStack } from "../error"; |
| 8 | + |
| 9 | +export function createErrorBoundary( |
| 10 | + render: () => VNode, |
| 11 | + renderErrors: (error: ErrorsDef) => VNode |
| 12 | +): VNode { |
| 13 | + return vueH(ErrorBoundary, { render, renderErrors }); |
| 14 | +} |
| 15 | + |
| 16 | +type ErrorBoundary = ComponentPublicInstance<{}, {}, ErrorBoundaryData>; |
| 17 | +type ErrorBoundaryData = { |
| 18 | + hasError: boolean; |
| 19 | + errorsDef: ErrorsDef | null; |
| 20 | +}; |
| 21 | +const ErrorBoundary = defineComponent<{ |
| 22 | + render: () => VNode; |
| 23 | + renderErrors: (error: ErrorsDef) => VNode; |
| 24 | +}>({ |
| 25 | + name: "ErrorBoundary", |
| 26 | + props: ["render", "renderErrors"], |
| 27 | + data: () => |
| 28 | + ({ |
| 29 | + hasError: false, |
| 30 | + errorsDef: null as ErrorsDef | null, |
| 31 | + }) as ErrorBoundaryData, |
| 32 | + setup({ render, renderErrors }) { |
| 33 | + return (ctx: ErrorBoundary) => |
| 34 | + ctx.$data.hasError && ctx.$data.errorsDef != null |
| 35 | + ? renderErrors(ctx.$data.errorsDef) |
| 36 | + : render(); |
| 37 | + }, |
| 38 | + errorCaptured(this: ErrorBoundary, err) { |
| 39 | + console.log("ERROR CAPTURED", err); |
| 40 | + this.$data.hasError = true; |
| 41 | + this.$data.errorsDef = errorToDef(err as Error); |
| 42 | + return false; |
| 43 | + }, |
| 44 | +}); |
| 45 | + |
| 46 | +function errorToDef(error: Error): ErrorsDef { |
| 47 | + const stack: ErrorStack[] = (error.stack?.split?.("\n") ?? []) |
| 48 | + .map(line => { |
| 49 | + if (line.length === 0) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + // split by really last @ or @http(s)?:// |
| 54 | + let [, name, source = ""] = line.match(/(.*)@(https?:\/\/.*)$/) ?? |
| 55 | + line.match(/(.*)@(:?[^@]*)$/) ?? [, line]; |
| 56 | + |
| 57 | + // replace empty string to anonymous call |
| 58 | + name = name || "<anonymous>"; |
| 59 | + |
| 60 | + // Ignore all the internal functions of react and vite |
| 61 | + if ( |
| 62 | + source.includes("vite/client") || |
| 63 | + name.includes("/node_modules/") || |
| 64 | + name.startsWith("__require") |
| 65 | + ) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + // Remove any URL prefix, leave just path |
| 70 | + source = source.startsWith(location.origin) |
| 71 | + ? source.substring(location.origin.length) |
| 72 | + : source; |
| 73 | + |
| 74 | + // Remove node_modules prefix from URL |
| 75 | + const NODE_MODULES_DEPS = "/node_modules/.vite/deps/"; |
| 76 | + source = source.startsWith(NODE_MODULES_DEPS) |
| 77 | + ? "/node_modules/" + |
| 78 | + source.substring(NODE_MODULES_DEPS.length).replace(/_/g, "/") |
| 79 | + : source; |
| 80 | + |
| 81 | + const SHOWCASE_DIST = "showcase/dist"; |
| 82 | + source = source.includes(SHOWCASE_DIST) |
| 83 | + ? "@showcase" + |
| 84 | + source.substring(source.indexOf(SHOWCASE_DIST) + SHOWCASE_DIST.length) |
| 85 | + : source; |
| 86 | + |
| 87 | + // match to <source-file>?<timestamp-or-version>:<line>:<column> |
| 88 | + const [, sourceFile, lineN, columnN] = source.match( |
| 89 | + /^(.+)?(?:t=\d+|v=\w+):(\d+):(\d+)$/ |
| 90 | + ) ?? [, source, "", ""]; |
| 91 | + |
| 92 | + if (sourceFile.endsWith("vue.js?")) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + return { |
| 97 | + line: parseInt(lineN), |
| 98 | + column: parseInt(columnN), |
| 99 | + name: name, |
| 100 | + source: sourceFile, |
| 101 | + } satisfies ErrorStack; |
| 102 | + }) |
| 103 | + .filter((e): e is ErrorStack => !!e); |
| 104 | + |
| 105 | + return { |
| 106 | + message: error.message, |
| 107 | + stack, |
| 108 | + }; |
| 109 | +} |
0 commit comments