Skip to content

Commit

Permalink
fix: correct socket address in use error message
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkdo committed Mar 19, 2019
1 parent b675a06 commit 2eb1965
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/server/src/listener.js
Expand Up @@ -95,15 +95,16 @@ export default class Listener {

// Use better error message
if (addressInUse) {
error.message = `Address \`${this.host}:${this.port}\` is already in use.`
}

// Listen to a random port on dev as a fallback
if (addressInUse && this.dev && this.port !== '0') {
consola.warn(error.message)
consola.info('Trying a random port...')
this.port = '0'
return this.close().then(() => this.listen())
const address = this.socket || `${this.host}:${this.port}`
error.message = `Address \`${address}\` is already in use.`

// Listen to a random port on dev as a fallback
if (this.dev && !this.socket && this.port !== '0') {
consola.warn(error.message)
consola.info('Trying a random port...')
this.port = '0'
return this.close().then(() => this.listen())
}
}

// Throw error
Expand Down
9 changes: 9 additions & 0 deletions packages/server/test/listener.test.js
Expand Up @@ -330,6 +330,15 @@ describe('server: listener', () => {
expect(() => listener.serverErrorHandler(addressInUse)).toThrow('Address `localhost:3000` is already in use.')
})

test('should throw address in use error for socket', () => {
const listener = new Listener({})
listener.socket = 'nuxt.socket'

const addressInUse = new Error()
addressInUse.code = 'EADDRINUSE'
expect(() => listener.serverErrorHandler(addressInUse)).toThrow('Address `nuxt.socket` is already in use.')
})

test('should fallback to a random port in address in use error', async () => {
const listener = new Listener({ dev: true })
listener.host = 'localhost'
Expand Down

0 comments on commit 2eb1965

Please sign in to comment.