Skip to content

Commit 7454937

Browse files
committed
fix(app-page-builder-elements): improve element error boundary
1 parent 8258879 commit 7454937

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

packages/app-page-builder-elements/src/components/Element.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const Element = (props: ElementProps) => {
3030
};
3131

3232
return (
33-
<ErrorBoundary>
33+
<ErrorBoundary element={element}>
3434
<ElementRenderer {...props} meta={meta} />
3535
</ErrorBoundary>
3636
);

packages/app-page-builder-elements/src/components/ErrorBoundary.tsx

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,73 @@
11
import React, { ErrorInfo } from "react";
2+
import { Element as ElementType } from "~/types";
3+
4+
type State =
5+
| {
6+
hasError: true;
7+
error: Error;
8+
}
9+
| { hasError: false; error: undefined };
210

3-
interface State {
4-
hasError: boolean;
5-
}
611
interface Props {
12+
element: ElementType;
713
[key: string]: any;
814
}
15+
16+
const displayNone = {
17+
display: "none"
18+
};
19+
20+
declare global {
21+
// eslint-disable-next-line
22+
namespace JSX {
23+
interface IntrinsicElements {
24+
"pb-element-error": any;
25+
}
26+
}
27+
}
28+
929
class ErrorBoundary extends React.Component<Props, State> {
1030
constructor(props: Props) {
1131
super(props);
1232
this.state = {
13-
hasError: false
33+
hasError: false,
34+
error: undefined
1435
};
1536
}
1637

17-
static getDerivedStateFromError() {
38+
static getDerivedStateFromError(error: Error) {
1839
// Update state so the next render will show the fallback UI.
1940
return {
20-
hasError: true
41+
hasError: true,
42+
error
2143
};
2244
}
2345

2446
public override componentDidCatch(error: Error, errorInfo: ErrorInfo) {
25-
console.log("An error occurred while rendering a page element:");
26-
console.log(error, errorInfo);
47+
const { element } = this.props;
48+
console.groupCollapsed(
49+
`%cELEMENT ERROR%c: An error occurred while rendering page element "${element.id}" of type "${element.type}".`,
50+
"color:red",
51+
"color:default"
52+
);
53+
console.log("element", element);
54+
console.error(error, errorInfo);
55+
console.groupEnd();
2756
}
2857

2958
public override render() {
3059
if (this.state.hasError) {
31-
// You can render any custom fallback UI
32-
return <h1>Something went wrong.</h1>;
60+
const elementData = {
61+
id: this.props.element.id,
62+
type: this.props.element.type,
63+
error: this.state.error.message
64+
};
65+
66+
return (
67+
<pb-element-error style={displayNone}>
68+
{JSON.stringify(elementData)}
69+
</pb-element-error>
70+
);
3371
}
3472

3573
return this.props.children;

0 commit comments

Comments
 (0)