Skip to content

Commit c3ef483

Browse files
dvcolombanantfubot
andauthored
feat(devframe): add onServerError for post-bind server errors (#165)
Co-authored-by: Anthony Fu (via agent) <reg-github-bot@antfu.me>
1 parent bf99de1 commit c3ef483

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

packages/devframe/src/node/server.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ export interface StartHttpAndWsOptions {
117117
* own startup banner. Devframe does not print one itself.
118118
*/
119119
onReady?: (info: { origin: string, port: number, app: H3 }) => void | Promise<void>
120+
/**
121+
* Called for any error the HTTP server devframe owns emits after it starts
122+
* listening — e.g. a transient `EMFILE` while accepting a connection.
123+
* Without it such an error has no listener and crashes the process.
124+
*
125+
* Applies only to a server devframe created itself. When `server` is
126+
* supplied the caller owns that object and attaches to it directly, so
127+
* devframe leaves its error handling alone.
128+
*/
129+
onServerError?: (error: Error) => void
120130
}
121131

122132
export interface StartedServer {
@@ -287,6 +297,10 @@ export async function startHttpAndWs(options: StartHttpAndWsOptions): Promise<St
287297
cause: error,
288298
})
289299
}
300+
// Attached only now that the bind has already succeeded — this never
301+
// changes bind-time crash/hang semantics, only what happens afterward.
302+
if (options.onServerError)
303+
httpServer.on('error', options.onServerError)
290304
}
291305

292306
const address = httpServer.address()

0 commit comments

Comments
 (0)