Skip to content

Commit

Permalink
feat: add customErrorInfo property to user errors (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Apr 14, 2022
1 parent 25ca13e commit 4a191df
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 4 deletions.
36 changes: 36 additions & 0 deletions src/bundle_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
interface BundleErrorOptions {
format: string
}

const getCustomErrorInfo = (options: BundleErrorOptions) => ({
location: {
format: options.format,
runtime: 'deno',
},
type: 'functionsBundling',
})

class BundleError extends Error {
customErrorInfo: ReturnType<typeof getCustomErrorInfo>

constructor(originalError: Error, options: BundleErrorOptions) {
super(originalError.message)

this.customErrorInfo = getCustomErrorInfo(options)
this.name = 'BundleError'
this.stack = originalError.stack

// https://github.com/microsoft/TypeScript-wiki/blob/8a66ecaf77118de456f7cd9c56848a40fe29b9b4/Breaking-Changes.md#implicit-any-error-raised-for-un-annotated-callback-arguments-with-no-matching-overload-arguments
Object.setPrototypeOf(this, BundleError.prototype)
}
}

const wrapBundleError = (input: unknown, options: BundleErrorOptions) => {
if (input instanceof Error) {
return new BundleError(input, options)
}

return input
}

export { BundleError, wrapBundleError }
4 changes: 2 additions & 2 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import type { Bundle } from './bundle.js'
import type { Declaration } from './declaration.js'
import { FeatureFlags, getFlags } from './feature_flags.js'
import { findFunctions } from './finder.js'
import { bundleESZIP } from './formats/eszip.js'
import { bundleJS } from './formats/javascript.js'
import { bundle as bundleESZIP } from './formats/eszip.js'
import { bundle as bundleJS } from './formats/javascript.js'
import { ImportMap, ImportMapFile } from './import_map.js'
import { writeManifest } from './manifest.js'

Expand Down
11 changes: 10 additions & 1 deletion src/formats/eszip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fileURLToPath } from 'url'

import { DenoBridge } from '../bridge.js'
import type { Bundle } from '../bundle.js'
import { wrapBundleError } from '../bundle_error.js'
import { EdgeFunction } from '../edge_function.js'
import { getFileHash } from '../utils/sha256.js'

Expand All @@ -15,6 +16,14 @@ 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 Down Expand Up @@ -52,4 +61,4 @@ const getESZIPBundler = () => {
return bundlerPath
}

export { bundleESZIP }
export { bundle }
11 changes: 10 additions & 1 deletion src/formats/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import del from 'del'

import { DenoBridge } from '../bridge.js'
import type { Bundle } from '../bundle.js'
import { wrapBundleError } from '../bundle_error.js'
import { EdgeFunction } from '../edge_function.js'
import { ImportMap } from '../import_map.js'
import type { FormatFunction } from '../server/server.js'
Expand All @@ -24,6 +25,14 @@ 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 Down Expand Up @@ -145,4 +154,4 @@ const getProductionEntryPoint = (functions: EdgeFunction[]) => {
return [bootImport, importLines, exportDeclaration, defaultExport].join('\n\n')
}

export { bundleJS, generateStage2, getBootstrapURL }
export { bundle, generateStage2, getBootstrapURL }
32 changes: 32 additions & 0 deletions test/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fileURLToPath } from 'url'
import test from 'ava'
import tmp from 'tmp-promise'

import { BundleError } from '../src/bundle_error.js'
import { bundle } from '../src/bundler.js'

const url = new URL(import.meta.url)
Expand Down Expand Up @@ -69,3 +70,34 @@ test('Produces an additional ESZIP bundle when the `edge_functions_produce_eszip

await fs.rmdir(tmpDir.path, { recursive: true })
})

test('Adds a custom error property to bundling errors', async (t) => {
const sourceDirectory = resolve(dirname, '..', 'fixtures', 'invalid_functions', 'functions')
const tmpDir = await tmp.dir()
const declarations = [
{
function: 'func1',
path: '/func1',
},
]

try {
await bundle([sourceDirectory], tmpDir.path, declarations)

t.fail('Expected bundling to throw')
} catch (error: unknown) {
if (error instanceof BundleError) {
t.deepEqual(error.customErrorInfo, {
location: {
format: 'javascript',
runtime: 'deno',
},
type: 'functionsBundling',
})
} else {
t.fail('Expected custom error')
}
} finally {
await fs.rmdir(tmpDir.path, { recursive: true })
}
})
1 change: 1 addition & 0 deletions test/fixtures/invalid_functions/functions/func1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default async () => new NotAResponse('Hello')

0 comments on commit 4a191df

Please sign in to comment.