fix: correct socket address in use error message

This commit is contained in:
Clark Du 2019-03-19 11:19:56 +00:00
parent b675a0623b
commit 2eb1965357
2 changed files with 18 additions and 8 deletions

View File

@ -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.`
}
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 (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())
// 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

View File

@ -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'