Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hide stack trace on syntax errors #464

Merged
merged 6 commits into from Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 11 additions & 1 deletion deno/bundle.ts
Expand Up @@ -3,4 +3,14 @@ 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) {
if (error.message.includes("The module's source code could not be parsed")) {
Copy link
Member

Choose a reason for hiding this comment

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

Could we not have a single if here?

if (error instanceof Error && error.message.includes("The module's source code could not be parsed")) {
  // (...)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

done in 10bbe18

delete error.stack
}
}

throw error
}
7 changes: 7 additions & 0 deletions node/bundle_error.ts
@@ -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")) {
Copy link
Member

Choose a reason for hiding this comment

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

Same here, if you don't mind.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that works - we want to wrap it in a BundleError, even if the message is something different

Copy link
Member

Choose a reason for hiding this comment

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

Oops, sorry. Didn't look at this properly.

// eslint-disable-next-line no-param-reassign
input.message = (input as ExecaError).stderr
}

return new BundleError(input, options)
}

Expand Down
12 changes: 11 additions & 1 deletion node/bundler.test.ts
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,15 @@ test('Adds a custom error property to user errors during bundling', async () =>
await bundle([sourceDirectory], distPath, declarations, { basePath })
} catch (error) {
expect(error).toBeInstanceOf(BundleError)
expect((error as BundleError).message).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));
Copy link
Member Author

Choose a reason for hiding this comment

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

I couldn't find a good way of removing these last couple lines :/ It's hard to pattern-match on this, because there's ANSI color modifiers in between the characters.

^
at <anonymous> (file:///Users/skn0tt/dev/netlify/edge-bundler/deno/vendor/deno.land/x/eszip@v0.40.0/eszip_wasm.generated.js:417:19)"
Copy link
Member

Choose a reason for hiding this comment

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

Your local paths will be a problem when matching the snapshot.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 656798d

`)
expect((error as BundleError).customErrorInfo).toEqual({
location: {
format: 'eszip',
Expand Down