From f47685c6020cf3a9b5cec21e2c6eef02c7303ec6 Mon Sep 17 00:00:00 2001 From: Mofei Zhang Date: Sun, 12 Nov 2023 14:56:10 -0500 Subject: [PATCH] [be] Flag and test for unexpected exceptions during compilations --- .../src/Entrypoint/Pipeline.ts | 9 ++++++++ .../src/HIR/Environment.ts | 6 +++++ ...le-unexpected-exception-pipeline.expect.md | 23 +++++++++++++++++++ ...or.handle-unexpected-exception-pipeline.ts | 8 +++++++ 4 files changed, 46 insertions(+) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.handle-unexpected-exception-pipeline.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.handle-unexpected-exception-pipeline.ts diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts index 9c2fe645f7..d28aa283ae 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Pipeline.ts @@ -361,6 +361,15 @@ function* runWithEnvironment( const ast = codegenReactiveFunction(reactiveFunction).unwrap(); yield log({ kind: "ast", name: "Codegen", value: ast }); + /** + * This flag should be only set for unit / fixture tests to check + * that Forget correctly handles unexpected errors (e.g. exceptions + * thrown by babel functions or other unexpected exceptions). + */ + if (env.config.throwUnknownException__testonly) { + throw new Error("unexpected error"); + } + return ast; } diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts index 25cd81de3f..40eca65b5e 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts @@ -281,6 +281,12 @@ const EnvironmentConfigSchema = z.object({ * Intended for use in demo purposes (incl playground) */ enableMemoizationComments: z.boolean().default(false), + + /** + * [TESTING ONLY] Throw an unknown exception during compilation to + * simulate unexpected exceptions e.g. errors from babel functions. + */ + throwUnknownException__testonly: z.boolean().default(false), }); export type EnvironmentConfig = z.infer; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.handle-unexpected-exception-pipeline.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.handle-unexpected-exception-pipeline.expect.md new file mode 100644 index 0000000000..996bae3a74 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.handle-unexpected-exception-pipeline.expect.md @@ -0,0 +1,23 @@ + +## Input + +```javascript +// @throwUnknownException__testonly:true + +function Component() {} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +}; + +``` + + +## Error + +``` +unexpected error +``` + + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.handle-unexpected-exception-pipeline.ts b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.handle-unexpected-exception-pipeline.ts new file mode 100644 index 0000000000..4a341c4e47 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.handle-unexpected-exception-pipeline.ts @@ -0,0 +1,8 @@ +// @throwUnknownException__testonly:true + +function Component() {} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], +};