From 9965db70bdb7b97a12e791e1463d67fe169c2b50 Mon Sep 17 00:00:00 2001 From: Mofei Zhang Date: Fri, 10 Nov 2023 17:09:29 -0500 Subject: [PATCH] Revert "[babel] Remove unused PipelineError" This reverts commit 10d129a8406e9d226abdb6943bf8512e34ce91db --- Reverts #2311 due to undocumented assumptions being broken. I also added some comments to `LoggerEvents` to explain each event type. In `Program.ts`, we have something like the following code. `compile` could produce any number of errors (not just expected errors / instances of `CompilerError`). As an example, we sometimes error in `Codegen` due to babel version incompatibilities (`Error: ObjectMethod: Too many arguments passed. Received 7 but can receive no more than 5`). ```js try { // any error could be thrown here compile(input); } catch (e) { // unknown type for e handleError(e, ...); } ``` --- .../src/Entrypoint/Options.ts | 5 ++++ .../src/Entrypoint/Program.ts | 23 +++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts index c8936f8032..438f918d5f 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts @@ -138,6 +138,11 @@ export type LoggerEvent = fnLoc: t.SourceLocation | null; fnName: string | null; memoSlots: number; + } + | { + kind: "PipelineError"; + fnLoc: t.SourceLocation | null; + data: any; }; export type Logger = { diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts index fb66c66f1e..18b92ee633 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts @@ -66,16 +66,31 @@ type CompileResult = { }; function handleError( - err: CompilerError, + err: unknown, pass: CompilerPass, fnLoc: t.SourceLocation | null ): void { if (pass.opts.logger) { - for (const detail of err.details) { + if (err instanceof CompilerError) { + for (const detail of err.details) { + pass.opts.logger.logEvent(pass.filename, { + kind: "CompileError", + fnLoc, + detail: detail.options, + }); + } + } else { + let stringifiedError; + if (err instanceof Error) { + stringifiedError = err.stack ?? err.message; + } else { + stringifiedError = err?.toString() ?? "[ null ]"; + } + pass.opts.logger.logEvent(pass.filename, { - kind: "CompileError", + kind: "PipelineError", fnLoc, - detail: detail.options, + data: stringifiedError, }); } }