Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

fix: refactor error annotation, to not loose original error #1152

Merged
merged 1 commit into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/runtimes/go/builder.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}
}