fix(custom-widgets): prevent editor crash on invalid v2 request manifest
The customJsx v2 serialize path called parseRequestManifest which threw on invalid JSON during React render (via getInput on every keystroke), crashing the page with Next.js 'Unexpected error' because the form had no error boundary. - Make parseRequestManifest defensive (try/catch -> empty array) - Wrap CustomWidgetForm in FormErrorBoundary (defense-in-depth) - Add unit tests for invalid manifest inputs
This commit is contained in:
1 parent
67d146220a
commit
9c31cdce37
6 files changed
+102
-19
No files matched your search
@@ -253,6 +253,12 @@ export function buildDisplayConfigFromFormValues(values: CustomWidgetDisplayForm
|
||||
}
|
||||
|
||||
function parseRequestManifest(value: string) {
|
||||
const parsed: unknown = JSON.parse(value);
|
||||
return customJsxRequestSchema.array().parse(parsed);
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(value);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
const result = customJsxRequestSchema.array().safeParse(parsed);
|
||||
return result.success ? result.data : [];
|
||||
}
|
||||
@@ -62,4 +62,16 @@ describe("custom-widget display form contracts", () => {
|
||||
requests: [{ id: "status", kind: "query", method: "GET" }],
|
||||
});
|
||||
});
|
||||
|
||||
test("does not throw on invalid request manifest JSON while serializing v2 form state", () => {
|
||||
const values = requiredFormValues("customJsx");
|
||||
values.jsxApiVersion = "2";
|
||||
|
||||
for (const invalidManifest of ["[invalid", "{not an array}", "", '{"id":1}', "null"]) {
|
||||
values.requestManifest = invalidManifest;
|
||||
expect(() => buildDisplayConfigFromFormValues(values)).not.toThrow();
|
||||
const config = buildDisplayConfigFromFormValues(values) as { requests?: unknown };
|
||||
expect(config.requests).toEqual([]);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user