Skip to content

Commit

Permalink
feat: port in use (#6887)
Browse files Browse the repository at this point in the history
* feat: more clear error messege

nforms the user that this could be from a netlify function opening ports

Co-authored-by: Thomas Lane <tlane25@users.noreply.github.com>

* fix: updated port in use error message

Co-authored-by: Thomas Lane <163203257+tlane25@users.noreply.github.com>

* fix: added types for EADDRINUSE error and updated error message
Co-authored-by: Will <wconrad265@users.noreply.github.com>

* fix: reverted back to commit before prettier and pnpm were installed
Co-authored-by: Thomas Lane <163203257+tlane25@users.noreply.github.com>

---------

Co-authored-by: Thomas Lane <tlane25@users.noreply.github.com>
Co-authored-by: Thomas Lane <163203257+tlane25@users.noreply.github.com>
Co-authored-by: t <t@ts-MacBook-Air.local>
  • Loading branch information
4 people authored Nov 1, 2024
1 parent 33ae41c commit 652f20a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
57 changes: 35 additions & 22 deletions src/commands/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,43 @@ import { createStatusCommand } from './status/index.js'
import { createSwitchCommand } from './switch/index.js'
import { createUnlinkCommand } from './unlink/index.js'
import { createWatchCommand } from './watch/index.js'

import { AddressInUseError } from './types.js'
const SUGGESTION_TIMEOUT = 1e4

process.on('uncaughtException', async (err) => {
console.log('')
error(
`${chalk.red(
'Netlify CLI has terminated unexpectedly',
)}\nThis is a problem with the Netlify CLI, not with your application.\nIf you recently updated the CLI, consider reverting to an older version by running:\n\n${chalk.bold(
'npm install -g netlify-cli@VERSION',
)}\n\nYou can use any version from ${chalk.underline(
'https://ntl.fyi/cli-versions',
)}.\n\nPlease report this problem at ${chalk.underline(
'https://ntl.fyi/cli-error',
)} including the error details below.\n`,
{ exit: false },
)

const systemInfo = await getSystemInfo()

console.log(chalk.dim(err.stack || err))
console.log(chalk.dim(systemInfo))

reportError(err, { severity: 'error' })
process.on('uncaughtException', async (err: AddressInUseError | Error) => {
if ('code' in err && err.code === 'EADDRINUSE') {
error(
`${chalk.red(`Port ${err.port} is already in use`)}\n\n` +
`Your serverless functions might be initializing a server\n` +
`to listen on specific port without properly closing it.\n\n` +
`This behavior is generally not advised\n` +
`To resolve this issue, try the following:\n` +
`1. If you NEED your serverless function to listen on a specific port,\n` +
`use a randomly assigned port as we do not officially support this.\n` +
`2. Review your serverless functions for lingering server connections, close them\n` +
`3. Check if any other applications are using port ${err.port}\n`,
{ exit: false },
)
} else {
error(
`${chalk.red(
'Netlify CLI has terminated unexpectedly',
)}\nThis is a problem with the Netlify CLI, not with your application.\nIf you recently updated the CLI, consider reverting to an older version by running:\n\n${chalk.bold(
'npm install -g netlify-cli@VERSION',
)}\n\nYou can use any version from ${chalk.underline(
'https://ntl.fyi/cli-versions',
)}.\n\nPlease report this problem at ${chalk.underline(
'https://ntl.fyi/cli-error',
)} including the error details below.\n`,
{ exit: false },
)

const systemInfo = await getSystemInfo()

console.log(chalk.dim(err.stack || err))
console.log(chalk.dim(systemInfo))
reportError(err, { severity: 'error' })
}

process.exit(1)
})
Expand Down
8 changes: 8 additions & 0 deletions src/commands/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ export type NetlifyOptions = {
state: StateConfig
frameworksAPIPaths: FrameworksAPIPaths
}

export interface AddressInUseError extends Error {
code: 'EADDRINUSE'
errno: number
syscall: 'listen'
address: string
port: number
}

0 comments on commit 652f20a

Please sign in to comment.