Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Rework ErrorBoundary
  • Loading branch information
lxsmnsyc committed Jan 11, 2024
commit 4faab526eab013f661aae11d26f27d875eb0b25f
32 changes: 22 additions & 10 deletions packages/solid/src/render/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,25 +256,37 @@ export function ErrorBoundary(props: {
fallback: JSX.Element | ((err: any, reset: () => void) => JSX.Element);
children: JSX.Element;
}): JSX.Element {
let err;
if (sharedConfig!.context && sharedConfig!.load)
err = sharedConfig.load(sharedConfig.context.id + sharedConfig.context.count);
let err,
hasError = false;
if (sharedConfig.context && sharedConfig.load && sharedConfig.has) {
const key = sharedConfig.context.id + sharedConfig.context.count;
hasError = sharedConfig.has(key);
err = sharedConfig.load(key);
}
const [errored, setErrored] = createSignal<any>(
err,
"_SOLID_DEV_" ? { name: "errored" } : undefined
"_SOLID_DEV_" ? { name: "errored", equals: false } : { equals: false }
);
const pushError = (action: any) => {
hasError = true;
setErrored(() => action);
};
const clearError = () => {
hasError = false;
setErrored();
};
Errors || (Errors = new Set());
Errors.add(setErrored);
onCleanup(() => Errors.delete(setErrored));
Errors.add(clearError as Setter<any>);
onCleanup(() => Errors.delete(clearError as Setter<any>));
return createMemo(
() => {
let e: any;
if ((e = errored())) {
const e = errored();
if (hasError) {
const f = props.fallback;
if ("_SOLID_DEV_" && (typeof f !== "function" || f.length == 0)) console.error(e);
return typeof f === "function" && f.length ? untrack(() => f(e, () => setErrored())) : f;
return typeof f === "function" && f.length ? untrack(() => f(e, clearError)) : f;
}
return catchError(() => props.children, setErrored);
return catchError(() => props.children, pushError);
},
undefined,
"_SOLID_DEV_" ? { name: "value" } : undefined
Expand Down
4 changes: 3 additions & 1 deletion packages/solid/src/server/rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ export function ErrorBoundary(props: {
children: string;
}) {
let error: any,
hasError = false,
res: any,
clean: any,
sync = true;
Expand All @@ -275,13 +276,14 @@ export function ErrorBoundary(props: {
return catchError(
() => (res = props.children),
err => {
hasError = true;
error = err;
!sync && ctx.replace("e" + id, displayFallback);
sync = true;
}
);
});
if (error) return displayFallback();
if (hasError) return displayFallback();
sync = false;
return { t: `<!--!$e${id}-->${resolveSSRNode(res)}<!--!$/e${id}-->` };
}
Expand Down