From efe053862ea702c82e4c12c048f53a1fd64489a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Thu, 30 Jun 2022 16:47:25 +0100 Subject: [PATCH] fix: improve user/system error boundaries (netlify/edge-bundler#63) --- packages/edge-bundler/src/formats/eszip.ts | 16 ++++++---------- packages/edge-bundler/src/formats/javascript.ts | 17 +++++++---------- packages/edge-bundler/test/bundler.ts | 13 ++++++++++++- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/packages/edge-bundler/src/formats/eszip.ts b/packages/edge-bundler/src/formats/eszip.ts index 34c9a3de97..a06033e86b 100644 --- a/packages/edge-bundler/src/formats/eszip.ts +++ b/packages/edge-bundler/src/formats/eszip.ts @@ -16,14 +16,6 @@ interface BundleESZIPOptions { functions: EdgeFunction[] } -const bundle = async (options: BundleESZIPOptions) => { - try { - return await bundleESZIP(options) - } catch (error: unknown) { - throw wrapBundleError(error, { format: 'eszip' }) - } -} - const bundleESZIP = async ({ basePath, buildID, @@ -46,7 +38,11 @@ const bundleESZIP = async ({ flags.push('--quiet') } - await deno.run(['run', ...flags, bundler, JSON.stringify(payload)], { pipeOutput: true }) + try { + await deno.run(['run', ...flags, bundler, JSON.stringify(payload)], { pipeOutput: true }) + } catch (error: unknown) { + throw wrapBundleError(error, { format: 'eszip' }) + } const hash = await getFileHash(destPath) @@ -61,4 +57,4 @@ const getESZIPBundler = () => { return bundlerPath } -export { bundle } +export { bundleESZIP as bundle } diff --git a/packages/edge-bundler/src/formats/javascript.ts b/packages/edge-bundler/src/formats/javascript.ts index 77af015817..b86ada783d 100644 --- a/packages/edge-bundler/src/formats/javascript.ts +++ b/packages/edge-bundler/src/formats/javascript.ts @@ -24,14 +24,6 @@ interface BundleJSOptions { importMap: ImportMap } -const bundle = async (options: BundleJSOptions) => { - try { - return await bundleJS(options) - } catch (error: unknown) { - throw wrapBundleError(error, { format: 'javascript' }) - } -} - const bundleJS = async ({ buildID, debug, @@ -49,7 +41,12 @@ const bundleJS = async ({ flags.push('--quiet') } - await deno.run(['bundle', ...flags, stage2Path, jsBundlePath], { pipeOutput: true }) + try { + await deno.run(['bundle', ...flags, stage2Path, jsBundlePath], { pipeOutput: true }) + } catch (error: unknown) { + throw wrapBundleError(error, { format: 'javascript' }) + } + await fs.unlink(stage2Path) const hash = await getFileHash(jsBundlePath) @@ -153,4 +150,4 @@ const getProductionEntryPoint = (functions: EdgeFunction[]) => { return [bootImport, importLines, exportDeclaration, defaultExport].join('\n\n') } -export { bundle, generateStage2, getBootstrapURL } +export { bundleJS as bundle, generateStage2, getBootstrapURL } diff --git a/packages/edge-bundler/test/bundler.ts b/packages/edge-bundler/test/bundler.ts index 7f6750ef9f..5776e0b0c9 100644 --- a/packages/edge-bundler/test/bundler.ts +++ b/packages/edge-bundler/test/bundler.ts @@ -70,7 +70,7 @@ test('Produces only a ESZIP bundle when the `edge_functions_produce_eszip` featu await fs.rmdir(tmpDir.path, { recursive: true }) }) -test('Adds a custom error property to bundling errors', async (t) => { +test('Adds a custom error property to user errors during bundling', async (t) => { const sourceDirectory = resolve(dirname, '..', 'fixtures', 'invalid_functions', 'functions') const tmpDir = await tmp.dir() const declarations = [ @@ -100,3 +100,14 @@ test('Adds a custom error property to bundling errors', async (t) => { await fs.rmdir(tmpDir.path, { recursive: true }) } }) + +test('Does not add a custom error property to system errors during bundling', async (t) => { + try { + // @ts-expect-error Sending bad input to `bundle` to force a system error. + await bundle([123, 321], tmpDir.path, declarations) + + t.fail('Expected bundling to throw') + } catch (error: unknown) { + t.false(error instanceof BundleError) + } +})