Skip to content

Commit

Permalink
fix(form): handle exceptions in form
Browse files Browse the repository at this point in the history
* Add default error boundary for form
* Allow users to provide a custom error boundary
  • Loading branch information
miguelgrc committed Nov 13, 2024
1 parent deb3720 commit 8e09624
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 45 deletions.
7 changes: 6 additions & 1 deletion formule-demo/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import ReactDOM from "react-dom/client";
import App from "./App.tsx";
import { ConfigProvider } from "antd";
import { theme } from "./theme.ts";
import { Alert } from "antd";

const { ErrorBoundary } = Alert;

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<ConfigProvider theme={theme}>
<App />
<ErrorBoundary>
<App />
</ErrorBoundary>
</ConfigProvider>
</React.StrictMode>,
);
2 changes: 1 addition & 1 deletion formule-demo/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3497,7 +3497,7 @@ react-dom@^18.2.0:
scheduler "^0.23.2"

"react-formule@file:..":
version "1.1.1"
version "1.2.0"
dependencies:
"@ant-design/pro-layout" "^7.16.4"
"@codemirror/lang-json" "^6.0.1"
Expand Down
5 changes: 4 additions & 1 deletion src/exposed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type FormuleContextProps = {
customPublishedWidgets?: object;
theme?: ThemeConfig;
separator?: string;
errorBoundary?: ReactNode;
synchronizeState?: (state: SchemaWizardState) => void;
transformSchema?: (schema: object) => object;
};
Expand All @@ -34,6 +35,7 @@ export const FormuleContext = ({
customPublishedWidgets,
theme,
separator = "::",
errorBoundary,
synchronizeState,
transformSchema = (schema) => schema,
}: FormuleContextProps) => {
Expand Down Expand Up @@ -61,8 +63,9 @@ export const FormuleContext = ({
customWidgets,
customPublishedFields,
customPublishedWidgets,
transformSchema,
separator,
errorBoundary,
transformSchema,
}}
>
{content}
Expand Down
89 changes: 47 additions & 42 deletions src/forms/Form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useContext } from "react";
import { Provider, useDispatch } from "react-redux";
import store from "../store/configureStore";
import { updateFormData } from "../store/schemaWizard";
import FormErrorBoundary from "./FormErrorBoundary";

const RJSFForm = ({
formRef,
Expand Down Expand Up @@ -54,50 +55,54 @@ const RJSFForm = ({
ObjectFieldTemplate: Objects || ObjectFieldTemplate,
};

const ErrorBoundary = customizationContext.errorBoundary || FormErrorBoundary;

return (
<Provider store={store}>
<Form
className={["__Form__", className].join(" ")}
ref={formRef}
schema={schema}
uiSchema={uiSchema}
tagName={tagName}
formData={formData}
fields={{
...CAPFields,
...customizationContext.customFields,
...fields,
...(isPublished && PublishedFields),
...(isPublished && customizationContext.customPublishedFields),
}}
widgets={{
...CAPWidgets,
...customizationContext.customWidgets,
...widgets,
...(isPublished && PublishedWidgets),
...(isPublished && customizationContext.customPublishedWidgets),
}}
templates={templates}
liveValidate={liveValidate}
showErrorList={showErrorList}
noHtml5Validate={true}
onError={() => {}}
onBlur={() => {}}
customValidate={validate}
validator={validator}
extraErrors={extraErrors}
onChange={handleChange}
readonly={readonly || isPublished}
transformErrors={transformErrors}
formContext={{
formRef,
...formContext,
hideAnchors,
}}
idSeparator={customizationContext.separator}
>
<span />
</Form>
<ErrorBoundary>
<Form
className={["__Form__", className].join(" ")}
ref={formRef}
schema={schema}
uiSchema={uiSchema}
tagName={tagName}
formData={formData}
fields={{
...CAPFields,
...customizationContext.customFields,
...fields,
...(isPublished && PublishedFields),
...(isPublished && customizationContext.customPublishedFields),
}}
widgets={{
...CAPWidgets,
...customizationContext.customWidgets,
...widgets,
...(isPublished && PublishedWidgets),
...(isPublished && customizationContext.customPublishedWidgets),
}}
templates={templates}
liveValidate={liveValidate}
showErrorList={showErrorList}
noHtml5Validate={true}
onError={() => {}}
onBlur={() => {}}
customValidate={validate}
validator={validator}
extraErrors={extraErrors}
onChange={handleChange}
readonly={readonly || isPublished}
transformErrors={transformErrors}
formContext={{
formRef,
...formContext,
hideAnchors,
}}
idSeparator={customizationContext.separator}
>
<span />
</Form>
</ErrorBoundary>
</Provider>
);
};
Expand Down
49 changes: 49 additions & 0 deletions src/forms/FormErrorBoundary.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Alert, Typography } from "antd";

const { ErrorBoundary } = Alert;

const FormErrorBoundary = ({ children }) => (
<ErrorBoundary
description={
<div style={{ whiteSpace: "normal" }}>
<Typography.Text strong>Why am I getting this error?</Typography.Text>
<Typography.Paragraph ellipsis={{ rows: 12, expandable: true }}>
Your schema might not be following the{" "}
<a
href="https://json-schema.org/specification"
target="_blank"
rel="noreferrer"
>
JSONSchema specification
</a>
. This usually happens when you have manually modified the JSON schema
and introduced some errors. Please make sure the schema follows the
specification and try again.
<br />
Notes:
<ul>
<li>
Formule adds some custom properties to the schema not present in
the JSONSchema specification, but these should not cause issues.
</li>
<li>
When you get this error, you usually want to be looking at clear
violations of the JSON Schema principles. For example, list or
object fields not containing a type or containing children as
direct descendants instead of within a <code>properties</code>
or <code>items</code> object.
</li>
<li>
These errors could also be coming from the uiSchema (e.g.
non-existing widget/field).
</li>
</ul>
</Typography.Paragraph>
</div>
}
>
{children}
</ErrorBoundary>
);

export default FormErrorBoundary;

0 comments on commit 8e09624

Please sign in to comment.