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

Handle contentType not being set in netlify dev #1083

Merged
merged 2 commits into from
Aug 12, 2020
Merged

Handle contentType not being set in netlify dev #1083

merged 2 commits into from
Aug 12, 2020

Conversation

yuvipanda
Copy link
Contributor

- Summary

netlify dev was failing for me consistently with the following error message:

◈ Netlify Dev ◈
◈ No app server detected and no "command" specified
◈ Using current working directory
◈ Unable to determine public folder to serve files from
◈ Setup a netlify.toml file with a [dev] section to specify your dev server settings.
◈ See docs at: https://cli.netlify.com/netlify-dev#project-detection
◈ Running static server from "mybinder.term"

◈ Server listening to 3999

   ┌─────────────────────────────────────────────────┐
   │                                                 │
   │   ◈ Server now ready on http://localhost:8888   │
   │                                                 │
   └─────────────────────────────────────────────────┘

TypeError: Cannot read property 'endsWith' of undefined
    at serveRedirect (/usr/local/lib/node_modules/netlify-cli/src/commands/dev/index.js:344:16)
TypeError: Cannot read property 'endsWith' of undefined
    at serveRedirect (/usr/local/lib/node_modules/netlify-cli/src/commands/dev/index.js:344:16)

/usr/local/lib/node_modules/netlify-cli/node_modules/netlify-redirector/lib/redirects.js:116
      throw ex;
      ^
abort({}) at Error
    at jsStackTrace (/usr/local/lib/node_modules/netlify-cli/node_modules/netlify-redirector/lib/redirects.js:1070:13)
    at stackTrace (/usr/local/lib/node_modules/netlify-cli/node_modules/netlify-redirector/lib/redirects.js:1087:12)
    at process.abort (/usr/local/lib/node_modules/netlify-cli/node_modules/netlify-redirector/lib/redirects.js:8502:44)
    at process.emit (events.js:314:20)
    at processPromiseRejections (internal/process/promises.js:245:33)
    at processTicksAndRejections (internal/process/task_queues.js:94:32)
(Use `node --trace-uncaught ...` to show where the exception was thrown)

The code was setting ct to be an empty object if no content-type header was
set, but then accessing .type on the object unconditionally. With this PR,
we set ct to be the content-type itself, avoiding any attribute access that
might cause undefined attribute access.

- Test plan

- Description for the changelog

  • Fixes netlify dev crashes when requests don't have content-type set

- A picture of a cute animal (not mandatory but encouraged)

@erezrokah erezrokah added the type: bug code to address defects in shipped code label Aug 10, 2020
@erezrokah erezrokah self-requested a review August 12, 2020 14:19
@erezrokah
Copy link
Contributor

Thanks @yuvipanda, can we move this from draft to ready?
This looks good to me and I added a test case to make sure I understand the issue.

yuvipanda and others added 2 commits August 12, 2020 16:32
`netlify dev` was failing for me consistently with the following error message:

```
◈ Netlify Dev ◈
◈ No app server detected and no "command" specified
◈ Using current working directory
◈ Unable to determine public folder to serve files from
◈ Setup a netlify.toml file with a [dev] section to specify your dev server settings.
◈ See docs at: https://cli.netlify.com/netlify-dev#project-detection
◈ Running static server from "mybinder.term"

◈ Server listening to 3999

   ┌─────────────────────────────────────────────────┐
   │                                                 │
   │   ◈ Server now ready on http://localhost:8888   │
   │                                                 │
   └─────────────────────────────────────────────────┘

TypeError: Cannot read property 'endsWith' of undefined
    at serveRedirect (/usr/local/lib/node_modules/netlify-cli/src/commands/dev/index.js:344:16)
TypeError: Cannot read property 'endsWith' of undefined
    at serveRedirect (/usr/local/lib/node_modules/netlify-cli/src/commands/dev/index.js:344:16)

/usr/local/lib/node_modules/netlify-cli/node_modules/netlify-redirector/lib/redirects.js:116
      throw ex;
      ^
abort({}) at Error
    at jsStackTrace (/usr/local/lib/node_modules/netlify-cli/node_modules/netlify-redirector/lib/redirects.js:1070:13)
    at stackTrace (/usr/local/lib/node_modules/netlify-cli/node_modules/netlify-redirector/lib/redirects.js:1087:12)
    at process.abort (/usr/local/lib/node_modules/netlify-cli/node_modules/netlify-redirector/lib/redirects.js:8502:44)
    at process.emit (events.js:314:20)
    at processPromiseRejections (internal/process/promises.js:245:33)
    at processTicksAndRejections (internal/process/task_queues.js:94:32)
(Use `node --trace-uncaught ...` to show where the exception was thrown)
```

The code was setting `ct` to be an empty object if no content-type header was
set, but then accessing `.type` on the object unconditionally. With this PR,
we set `ct` to be the content-type itself, avoiding any attribute access that
might cause undefined attribute access.
@yuvipanda yuvipanda marked this pull request as ready for review August 12, 2020 15:06
@yuvipanda
Copy link
Contributor Author

Thank you! I was putting off writing a test case - thanks for doing that :)

@erezrokah erezrokah merged commit 23d290a into netlify:master Aug 12, 2020
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

Successfully merging this pull request may close these issues.

2 participants