generated from LankyMoose/pnpm-monorepo-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve all kaioken errors, expose Signal.make(Readonly|Writable)
- Loading branch information
1 parent
ef063a9
commit 5d63345
Showing
10 changed files
with
317 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,82 @@ | ||
import { kaiokenErrorSymbol } from "./constants.js" | ||
import { __DEV__ } from "./env.js" | ||
import { findParent, noop } from "./utils.js" | ||
|
||
type KaiokenErrorOptions = | ||
| string | ||
| { | ||
message: string | ||
/** Used to indicate that the error is fatal and should crash the application */ | ||
fatal?: boolean | ||
/** Used to generate custom node stack */ | ||
vNode?: Kaioken.VNode | ||
} | ||
|
||
export class KaiokenError extends Error { | ||
[kaiokenErrorSymbol] = true | ||
/** Indicates whether the error is fatal and should crash the application */ | ||
fatal?: boolean | ||
/** Present if vNode is provided */ | ||
customNodeStack?: string | ||
constructor(optionsOrMessage: KaiokenErrorOptions) { | ||
const message = | ||
typeof optionsOrMessage === "string" | ||
? optionsOrMessage | ||
: optionsOrMessage.message | ||
super(message) | ||
if (typeof optionsOrMessage !== "string") { | ||
if (optionsOrMessage?.vNode) { | ||
this.customNodeStack = captureErrorStack(optionsOrMessage.vNode) | ||
} | ||
this.fatal = optionsOrMessage?.fatal | ||
} | ||
} | ||
|
||
static isKaiokenError(error: unknown): error is KaiokenError { | ||
return ( | ||
error instanceof Error && | ||
(error as KaiokenError)[kaiokenErrorSymbol] === true | ||
) | ||
} | ||
} | ||
|
||
function captureErrorStack(vNode: Kaioken.VNode) { | ||
let n = vNode | ||
let componentFns: string[] = [] | ||
while (n) { | ||
if (!n.parent) break // skip root node | ||
if (typeof n.type === "function") { | ||
componentFns.push(getComponentErrorDisplayText(n.type)) | ||
} else if (typeof n.type === "string") { | ||
componentFns.push(n.type) | ||
} | ||
n = n.parent | ||
} | ||
const componentNode = ( | ||
typeof vNode.type === "function" | ||
? vNode | ||
: findParent(vNode, (n) => typeof n.type === "function") | ||
) as (Kaioken.VNode & { type: Function }) | undefined | ||
return `The above error occurred in the <${getFunctionName(componentNode?.type || noop)}> component: | ||
${componentFns.map((x) => ` at ${x}`).join("\n")}\n` | ||
} | ||
|
||
function getComponentErrorDisplayText(fn: Function) { | ||
let str = getFunctionName(fn) | ||
if (__DEV__) { | ||
const fileLink = getComponentFileLink(fn) | ||
if (fileLink) { | ||
str = `${str} (${fileLink})` | ||
} | ||
} | ||
return str | ||
} | ||
|
||
function getFunctionName(fn: Function) { | ||
return (fn as any).displayName ?? (fn.name || "Anonymous Function") | ||
} | ||
|
||
function getComponentFileLink(fn: Function) { | ||
return fn.toString().match(/\/\/ \[kaioken_devtools\]:(.*)/)?.[1] ?? null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.