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

CLI cannot handle non Error-typed thrown errors #6753

Open
tdbit opened this issue Jul 10, 2024 · 0 comments
Open

CLI cannot handle non Error-typed thrown errors #6753

tdbit opened this issue Jul 10, 2024 · 0 comments
Labels
type: bug code to address defects in shipped code

Comments

@tdbit
Copy link

tdbit commented Jul 10, 2024

Describe the bug

The CLI will crash if a function throws a non Error type. It is good practice to always throw an Error type but it is not always guaranteed. Developers might throw custom error types, their own objects, or even (in our case) a simple string. This is perhaps not great engineering but it happens. When that non-standard error type is thrown inside a Netlify function it crashes the CLI as follows:

TypeError: Cannot read properties of undefined (reading 'map')
    at getNormalizedError (file:///Users/tdbit/.nvm/versions/node/v20.9.0/lib/node_modules/netlify-cli/dist/lib/functions/synchronous.js:88:41)
    at handleSynchronousFunction (file:///Users/tdbit/.nvm/versions/node/v20.9.0/lib/node_modules/netlify-cli/dist/lib/functions/synchronous.js:33:23)
    at handler (file:///Users/tdbit/.nvm/versions/node/v20.9.0/lib/node_modules/netlify-cli/dist/lib/functions/server.js:203:13)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

  System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 10.1.0 - ~/.nvm/versions/node/v20.9.0/bin/npm
  Browsers:
    Chrome: 126.0.6478.127
    Safari: 17.5
  npmGlobalPackages:
    netlify-cli: 17.33.0

This is because getNormalizedError assumes the error will have a .stackTrace property.

Steps to reproduce

  1. Add new Netlify function baderror.js to project:
export async function handler(event, context) {
throw "What?"
}
  1. Run netlify dev
  2. Hit localhost:8080/.netlify/functions/baderror

Configuration

No response

Environment

System:
OS: macOS 14.5
CPU: (10) arm64 Apple M1 Max
Memory: 5.24 GB / 64.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
Yarn: 1.22.22 - /opt/homebrew/bin/yarn
npm: 10.1.0 - ~/.nvm/versions/node/v20.9.0/bin/npm
npmGlobalPackages:
netlify-cli: 17.33.0

@tdbit tdbit added the type: bug code to address defects in shipped code label Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

1 participant