Skip to content

Commit

Permalink
fix: refactor error annotation, to not loose original error (#1152)
Browse files Browse the repository at this point in the history
With the previous approach for example `code` was lost
Instead of wrapping the error we now only annotate it
  • Loading branch information
danez committed Jul 19, 2022
1 parent eb1d3dd commit adc08c5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/runtimes/go/builder.ts
Expand Up @@ -19,7 +19,7 @@ export const build = async ({ destPath, mainFile, srcDir }: { destPath: string;
} catch (error) {
console.error(`Could not compile Go function ${functionName}:\n`)

throw new FunctionBundlingUserError(error, { functionName, runtime: 'go' })
throw FunctionBundlingUserError.addCustomErrorInfo(error, { functionName, runtime: 'go' })
}

const stat = await fs.lstat(destPath)
Expand Down
2 changes: 1 addition & 1 deletion src/runtimes/node/bundlers/esbuild/bundler.ts
Expand Up @@ -133,7 +133,7 @@ export const bundleJsFile = async function ({
warnings,
}
} catch (error) {
throw new FunctionBundlingUserError(error, { functionName: name, runtime: 'js', bundler: 'esbuild' })
throw FunctionBundlingUserError.addCustomErrorInfo(error, { functionName: name, runtime: 'js', bundler: 'esbuild' })
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtimes/node/bundlers/nft/transpile.ts
Expand Up @@ -23,6 +23,6 @@ export const transpile = async (path: string, config: FunctionConfig, functionNa

return transpiled.outputFiles[0].text
} catch (error) {
throw new FunctionBundlingUserError(error, { functionName, runtime: 'js', bundler: 'nft' })
throw FunctionBundlingUserError.addCustomErrorInfo(error, { functionName, runtime: 'js', bundler: 'nft' })
}
}
2 changes: 1 addition & 1 deletion src/runtimes/node/bundlers/zisi/list_imports.ts
Expand Up @@ -57,7 +57,7 @@ export const listImports = async ({
target: 'esnext',
})
} catch (error) {
throw new FunctionBundlingUserError(error, { functionName, runtime: 'js', bundler: 'zisi' })
throw FunctionBundlingUserError.addCustomErrorInfo(error, { functionName, runtime: 'js', bundler: 'zisi' })
} finally {
await safeUnlink(targetPath)
}
Expand Down
4 changes: 2 additions & 2 deletions src/runtimes/rust/builder.ts
Expand Up @@ -17,7 +17,7 @@ export const build = async ({ config, name, srcDir }: { config: FunctionConfig;
try {
await installToolchainOnce()
} catch (error) {
throw new FunctionBundlingUserError(error, { functionName, runtime: 'rs' })
throw FunctionBundlingUserError.addCustomErrorInfo(error, { functionName, runtime: 'rs' })
}

const targetDirectory = await getTargetDirectory({ config, name })
Expand Down Expand Up @@ -67,7 +67,7 @@ const cargoBuild = async ({
'There is no Rust toolchain installed. Visit https://ntl.fyi/missing-rust-toolchain for more information.'
}

throw new FunctionBundlingUserError(error, { functionName, runtime: 'rs' })
throw FunctionBundlingUserError.addCustomErrorInfo(error, { functionName, runtime: 'rs' })
}
}

Expand Down
27 changes: 16 additions & 11 deletions src/utils/error.ts
Expand Up @@ -12,22 +12,27 @@ interface CustomErrorInfo {
location: CustomErrorLocation
}

export class FunctionBundlingUserError extends Error {
customErrorInfo: CustomErrorInfo

constructor(messageOrError: string | Error, customErrorInfo: CustomErrorLocation) {
const isError = messageOrError instanceof Error
type UserError = Error & { customErrorInfo: CustomErrorInfo }

super(isError ? messageOrError.message : messageOrError)
export class FunctionBundlingUserError extends Error {
constructor(message: string, customErrorInfo: CustomErrorLocation) {
super(message)

Object.setPrototypeOf(this, new.target.prototype)
this.name = 'FunctionBundlingUserError'
if (isError) {
this.stack = messageOrError.stack
} else {
Error.captureStackTrace(this, FunctionBundlingUserError)
Error.captureStackTrace(this, FunctionBundlingUserError)

FunctionBundlingUserError.addCustomErrorInfo(this, customErrorInfo)
}

static addCustomErrorInfo(error: Error, customErrorInfo: CustomErrorLocation): UserError {
const info: CustomErrorInfo = {
type: 'functionsBundling',
location: customErrorInfo,
}

this.customErrorInfo = { type: 'functionsBundling', location: customErrorInfo }
;(error as UserError).customErrorInfo = info

return error as UserError
}
}

1 comment on commit adc08c5

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱ Benchmark results

largeDepsEsbuild: 6.3s

largeDepsNft: 30s

largeDepsZisi: 44.9s

Please sign in to comment.