Skip to content

Commit

Permalink
fix: hide stack trace on syntax errors (#464)
Browse files Browse the repository at this point in the history
* fix: hide stack trace on syntax errors

* fix: ts error

* fix: don't snapshot stacktrace

* Update node/bundler.test.ts

Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>

* refactor: use single if

---------

Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>
  • Loading branch information
Skn0tt and eduardoboucas authored Sep 6, 2023
1 parent 19d142d commit 9261b8c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
10 changes: 9 additions & 1 deletion deno/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ import { writeStage2 } from './lib/stage2.ts'
const [payload] = Deno.args
const { basePath, destPath, externals, functions, importMapData } = JSON.parse(payload)

await writeStage2({ basePath, destPath, externals, functions, importMapData })
try {
await writeStage2({ basePath, destPath, externals, functions, importMapData })
} catch (error) {
if (error instanceof Error && error.message.includes("The module's source code could not be parsed")) {
delete error.stack
}

throw error
}
7 changes: 7 additions & 0 deletions node/bundle_error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ExecaError } from 'execa'

interface BundleErrorOptions {
format: string
}
Expand Down Expand Up @@ -30,6 +32,11 @@ class BundleError extends Error {
*/
const wrapBundleError = (input: unknown, options?: BundleErrorOptions) => {
if (input instanceof Error) {
if (input.message.includes("The module's source code could not be parsed")) {
// eslint-disable-next-line no-param-reassign
input.message = (input as ExecaError).stderr
}

return new BundleError(input, options)
}

Expand Down
13 changes: 12 additions & 1 deletion node/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ test('Uses the vendored eszip module instead of fetching it from deno.land', asy
})

test('Adds a custom error property to user errors during bundling', async () => {
expect.assertions(2)
process.env.NO_COLOR = 'true'
expect.assertions(3)

const { basePath, cleanup, distPath } = await useFixture('invalid_functions')
const sourceDirectory = join(basePath, 'functions')
Expand All @@ -102,6 +103,16 @@ test('Adds a custom error property to user errors during bundling', async () =>
await bundle([sourceDirectory], distPath, declarations, { basePath })
} catch (error) {
expect(error).toBeInstanceOf(BundleError)
const [messageBeforeStack] = (error as BundleError).message.split('at <anonymous> (file://')
expect(messageBeforeStack).toMatchInlineSnapshot(`
"error: Uncaught (in promise) Error: The module's source code could not be parsed: Unexpected eof at file:///root/functions/func1.ts:1:27
export default async () =>
~
const ret = new Error(getStringFromWasm0(arg0, arg1));
^
"
`)
expect((error as BundleError).customErrorInfo).toEqual({
location: {
format: 'eszip',
Expand Down

0 comments on commit 9261b8c

Please sign in to comment.