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: offer a new port and listen if already used, use consola on server error #4428

Merged
merged 26 commits into from Nov 30, 2018
Merged

fix: offer a new port and listen if already used, use consola on server error #4428

merged 26 commits into from Nov 30, 2018

Conversation

ricardogobbosouza
Copy link
Contributor

@ricardogobbosouza ricardogobbosouza commented Nov 26, 2018

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

  • If port is already in use, offer a new and listen
  • Use consola on server error

Resolves: #4421

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly. (PR: #)
  • I have added tests to cover my changes (if not applicable, please state why)
  • All new and existing tests are passing.

@pi0 pi0 requested a review from manniL November 26, 2018 19:51
@codecov-io
Copy link

codecov-io commented Nov 26, 2018

Codecov Report

Merging #4428 into dev will increase coverage by 0.08%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             dev    #4428      +/-   ##
=========================================
+ Coverage   91.4%   91.48%   +0.08%     
=========================================
  Files         56       56              
  Lines       1943     1962      +19     
  Branches     485      487       +2     
=========================================
+ Hits        1776     1795      +19     
  Misses       155      155              
  Partials      12       12
Impacted Files Coverage Δ
packages/server/src/server.js 100% <ø> (ø) ⬆️
packages/server/src/listener.js 98.11% <100%> (+1.05%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ee13925...6e10375. Read the comment docs.

@manniL
Copy link
Member

manniL commented Nov 27, 2018

Before

image

Current (after that PR)

image

consola.fatal will actually terminate the process because of https://github.com/nuxt/nuxt.js/blob/7c4e77ffb9d18279bd9d0c5f4d39eb55569c6a81/packages/cli/src/setup.js#L25-L42. So we might should roll with consola.error(e)

With suggested changes

image

galvez
galvez previously approved these changes Nov 27, 2018
Copy link

@galvez galvez left a comment

Choose a reason for hiding this comment

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

Need to change to consola.error() like @manniL suggested but otherwise LGTM!

galvez
galvez previously approved these changes Nov 27, 2018
@Atinux
Copy link
Member

Atinux commented Nov 27, 2018

I think we can improve more the DX on this.

Why not checking the ECODE and display a better error instead when we know it?

@manniL
Copy link
Member

manniL commented Nov 27, 2018

@Atinux Had the same idea.

@ricardogobbosouza Do you want to take that or should we?

@ricardogobbosouza
Copy link
Contributor Author

@manniL, @Atinux, I thought of something like this:

this._server.on('error', (e) => {
  if (e.code === 'EADDRINUSE') {
    return consola.error(`Address already in use \`${this.host}:${this.port}\``)
  }

  consola.error(e)
}

What do you think?

@manniL
Copy link
Member

manniL commented Nov 27, 2018

@ricardogobbosouza Maybe like

Address \`${this.host}:${this.port}\` is already in use. Do you run another service on the same port?

We could do that for more common codes as well (list of all: http://man7.org/linux/man-pages/man3/errno.3.html)

@ricardogobbosouza
Copy link
Contributor Author

What will we customize? Everyone would be unnecessary, do not you think?

@manniL
Copy link
Member

manniL commented Nov 27, 2018

@ricardogobbosouza Not every single one ☺️
Maybe EDQUOT and EACCES as well?
What do you think @pi0? ☺️

@pi0
Copy link
Member

pi0 commented Nov 27, 2018

@manniL @ricardogobbosouza V8 error codes are listed here. Honestly, I would like to add this as a feature into Consola to translate error.codes. This PR is already good as is.

Something else as a suggestion: For development mode, like other frameworks (CRA, Next,..) why not gently offer a new port and listen on it? :D

@ricardogobbosouza
Copy link
Contributor Author

@pi0 I fully agree that the error handling is done in the consola and that a new port should be suggested to the user.
I'm making this improvement...

@ricardogobbosouza ricardogobbosouza changed the title fix: use consola on server error feat: offer a new port and listen, use consola on server error Nov 27, 2018
@ricardogobbosouza ricardogobbosouza changed the title feat: offer a new port and listen, use consola on server error fix: offer a new port and listen if already used, use consola on server error Nov 27, 2018
@HugoHeneault
Copy link

HugoHeneault commented Nov 28, 2018

@manniL Nice idea. But won't it be annoying to change ports at each nuxt restart? We need to restart it when we change nuxt.config.js and so we should update our tabs port? Would be nicer to take 3001 if 3000 is already taken, then 3002, etc.

@manniL
Copy link
Member

manniL commented Nov 28, 2018

@HugoHeneault Well, get-port would also choose a random port 🤷‍♂️
But I see your point there 🤔

@ricardogobbosouza
Copy link
Contributor Author

@HugoHeneault I think a random port is better than creating a counter for it.
Because if the defined port is already in use, the consola will warn.

@ricardogobbosouza
Copy link
Contributor Author

@pi0 @manniL @Atinux
What do you think? Better to put on an counter?
For me a random port resolves well

@manniL
Copy link
Member

manniL commented Nov 29, 2018

I'm fine with a random port if the initial one is blocked 👍


async serverErrorHandler(e) {
if (e.code === 'EADDRINUSE') {
consola.warn(`Address \`${this.host}:${this.port}\` is already in use.`)
Copy link
Member

Choose a reason for hiding this comment

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

For production, we have to throw a FATAL error because the server is not usable and we silently continue listening on a random port. This makes production debug hard

@pi0
Copy link
Member

pi0 commented Nov 29, 2018

@ricardogobbosouza I've made some improvement. Would you please review latest diff?

}

// Throw error
throw error
Copy link
Member

@pi0 pi0 Nov 29, 2018

Choose a reason for hiding this comment

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

Tip: This error is handled by consola when using cli. The reason why not using consola here is that programatic usage needs to capture the listen error instead of silently skipping

@pi0
Copy link
Member

pi0 commented Nov 29, 2018

Dev:

image

Prod:

image

@ricardogobbosouza
Copy link
Contributor Author

@pi0 nice!! 👍

@manniL
Copy link
Member

manniL commented Nov 29, 2018

🔥 🔥 🔥

@Atinux Atinux merged commit 1d78027 into nuxt:dev Nov 30, 2018
@lock
Copy link

lock bot commented Dec 30, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 30, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nuxt could check if localhost is free
8 participants