Skip to content

Commit

Permalink
fix up top level errorboundary
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed May 29, 2024
1 parent 0ac15a2 commit e0af541
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-swans-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

fix up top level errorboundary
6 changes: 3 additions & 3 deletions packages/start/src/server/StartServer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ssr,
useAssets
} from "solid-js/web";
import { ErrorBoundary } from "../shared/ErrorBoundary";
import { ErrorBoundary, TopErrorBoundary } from "../shared/ErrorBoundary";
import { renderAsset } from "./renderAsset";
import type { Asset, DocumentComponentProps, PageEvent } from "./types";

Expand Down Expand Up @@ -74,7 +74,7 @@ export function StartServer(props: { document: Component<DocumentComponentProps>
return (
<NoHydration>
{docType as unknown as any}
<ErrorBoundary>
<TopErrorBoundary>
<props.document
assets={
<>
Expand Down Expand Up @@ -128,7 +128,7 @@ export function StartServer(props: { document: Component<DocumentComponentProps>
</ErrorBoundary>
)}
</props.document>
</ErrorBoundary>
</TopErrorBoundary>
</NoHydration>
);
}
6 changes: 3 additions & 3 deletions packages/start/src/server/spa/StartServer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// @ts-ignore
import type { Component } from "solid-js";
import { NoHydration, getRequestEvent, ssr } from "solid-js/web";
import { TopErrorBoundary } from "../../shared/ErrorBoundary";
import { renderAsset } from "../renderAsset";
import type { DocumentComponentProps, PageEvent } from "../types";
import { ErrorBoundary } from "../../shared/ErrorBoundary";

const docType = ssr("<!DOCTYPE html>");

Expand All @@ -19,7 +19,7 @@ export function StartServer(props: { document: Component<DocumentComponentProps>
return (
<NoHydration>
{docType as unknown as any}
<ErrorBoundary>
<TopErrorBoundary>
<props.document
assets={<>{context.assets.map((m: any) => renderAsset(m))}</>}
scripts={
Expand Down Expand Up @@ -53,7 +53,7 @@ export function StartServer(props: { document: Component<DocumentComponentProps>
)
}
/>
</ErrorBoundary>
</TopErrorBoundary>
</NoHydration>
);
}
70 changes: 47 additions & 23 deletions packages/start/src/shared/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
// @refresh skip
import { ErrorBoundary as DefaultErrorBoundary, type ParentProps } from "solid-js";
import { ErrorBoundary as DefaultErrorBoundary, catchError, type ParentProps } from "solid-js";
import { isServer } from "solid-js/web";
import { HttpStatusCode } from "./HttpStatusCode";
import { DevOverlay } from "./dev-overlay";

export const ErrorBoundary = import.meta.env.DEV && import.meta.env.START_DEV_OVERLAY
? (props: ParentProps) => <DevOverlay>{props.children}</DevOverlay>
: (props: ParentProps) => {
const message = isServer ? "500 | Internal Server Error" : "Error | Uncaught Client Exception";
return (
<DefaultErrorBoundary
fallback={error => {
console.error(error);
return (
<>
<span style="font-size:1.5em;text-align:center;position:fixed;left:0px;bottom:55%;width:100%;">
{message}
</span>
<HttpStatusCode code={500} />
</>
);
}}
>
{props.children}
</DefaultErrorBoundary>
)
};
export const ErrorBoundary =
import.meta.env.DEV && import.meta.env.START_DEV_OVERLAY
? (props: ParentProps) => <DevOverlay>{props.children}</DevOverlay>
: (props: ParentProps) => {
const message = isServer
? "500 | Internal Server Error"
: "Error | Uncaught Client Exception";
return (
<DefaultErrorBoundary
fallback={error => {
console.error(error);
return (
<>
<span style="font-size:1.5em;text-align:center;position:fixed;left:0px;bottom:55%;width:100%;">
{message}
</span>
<HttpStatusCode code={500} />
</>
);
}}
>
{props.children}
</DefaultErrorBoundary>
);
};

export const TopErrorBoundary = (props: ParentProps) => {

This comment has been minimized.

Copy link
@LukasGerm

LukasGerm May 30, 2024

Contributor

Ah look at that, nice, thanks for fixing. Why exactly is this now better? What does this improve? :D trying to understand @ryansolid

let isError = false;
const res = catchError(
() => props.children,
err => {
console.error(err);
isError = !!err;
}
);
return isError ? (
<>
<span style="font-size:1.5em;text-align:center;position:fixed;left:0px;bottom:55%;width:100%;">
500 | Internal Server Error
</span>
<HttpStatusCode code={500} />
</>
) : (
res
);
};

0 comments on commit e0af541

Please sign in to comment.