Skip to content

Commit

Permalink
fix: added types for EADDRINUSE error and updated error message
Browse files Browse the repository at this point in the history
Co-authored-by: Will <[email protected]>
  • Loading branch information
t authored and t committed Oct 22, 2024
1 parent 9bd658a commit 34b6bb4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/commands/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,23 @@ 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: any) => {
console.log('')

if (err.code === 'EADDRINUSE') {
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` +
`This could be due to one of your serverless functions initializing a server\n` +
`to listen on port ${err.port} without properly closing it.\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. Check if any other applications are using port ${err.port}\n` +
`2. Review your serverless functions for any lingering server connections\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 {
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 34b6bb4

Please sign in to comment.