Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle PB Element plugin errors during render #726

Merged
merged 6 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion packages/app-page-builder/src/editor/components/Element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
transitionStyles,
typeStyle
} from "./Element/ElementStyled";
import tryRenderingPlugin from "./../../utils/tryRenderingPlugin";

export type ElementProps = {
className?: string;
Expand Down Expand Up @@ -100,6 +101,8 @@ const Element = (props: ElementProps) => {
return null;
}

const renderedPlugin = tryRenderingPlugin(() => plugin.render({ element }));

return (
<Transition in={true} timeout={250} appear={true}>
{state => (
Expand All @@ -115,7 +118,7 @@ const Element = (props: ElementProps) => {
<Draggable target={plugin.target} beginDrag={beginDrag} endDrag={endDrag}>
{renderDraggable}
</Draggable>
{plugin.render({ element })}
{renderedPlugin}
Pavel910 marked this conversation as resolved.
Show resolved Hide resolved
</div>
{/*
<div className="add-element add-element--above">+</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import ConnectedElement from "@webiny/app-page-builder/editor/components/Connect
import { ElementRoot } from "@webiny/app-page-builder/render/components/ElementRoot";
import RowContainer from "./RowContainer";
import ElementAnimation from "@webiny/app-page-builder/render/components/ElementAnimation";

Pavel910 marked this conversation as resolved.
Show resolved Hide resolved
/**
* TODO: this entire component can be further optimized (see the Chrome Profiler) to avoid unnecessary elements re-renders.
*/
Expand Down
6 changes: 5 additions & 1 deletion packages/app-page-builder/src/render/components/Element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import React, { useMemo } from "react";
import { getPlugins } from "@webiny/plugins";
import { PbElement, PbRenderElementPlugin, PbThemePlugin } from "@webiny/app-page-builder/types";

import tryRenderingPlugin from "./../../utils/tryRenderingPlugin";

export type ElementProps = {
element: PbElement;
};
Expand All @@ -27,7 +29,9 @@ const Element = (props: ElementProps) => {
return null;
}

return <>{plugin.render({ theme, element })}</>;
const renderedPlugin = tryRenderingPlugin(() => plugin.render({ theme, element }));

return <>{renderedPlugin}</>;
};

export default Element;
19 changes: 19 additions & 0 deletions packages/app-page-builder/src/utils/tryRenderingPlugin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";

const tryRenderingPlugin = fn => {
try {
return fn();
} catch (err) {
console.log(err);
return (
<div style={{ paddingTop: "15px", paddingBottom: "15px", color: "rgb(206, 17, 38)" }}>
<div style={{ fontSize: "1.5em", paddingBottom: "5px" }}>Error: {err.message}</div>
<div style={{ fontSize: "1.1em", color: "rgb(206, 17, 38)" }}>
Check the console for more information regarding the error.
</div>
</div>
);
}
};

export default tryRenderingPlugin;