Skip to content

Commit

Permalink
fix: improve user/system error boundaries (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Jun 30, 2022
1 parent 7214e02 commit a7ac87a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
16 changes: 6 additions & 10 deletions src/formats/eszip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand All @@ -61,4 +57,4 @@ const getESZIPBundler = () => {
return bundlerPath
}

export { bundle }
export { bundleESZIP as bundle }
17 changes: 7 additions & 10 deletions src/formats/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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 }
13 changes: 12 additions & 1 deletion test/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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)
}
})

0 comments on commit a7ac87a

Please sign in to comment.