From d5a37a18bd5e41d7a08b214f06481f830414def3 Mon Sep 17 00:00:00 2001 From: ochafik Date: Sun, 11 Jan 2026 13:33:43 +0000 Subject: [PATCH] fix: add error handling for server startup failures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, if app.listen() failed (e.g., port already in use), the server would fail silently. This adds proper error handling to log the error and exit with a non-zero status code. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- examples/basic-server-preact/server-utils.ts | 6 +++++- examples/basic-server-react/server-utils.ts | 6 +++++- examples/basic-server-solid/server-utils.ts | 6 +++++- examples/basic-server-svelte/server-utils.ts | 6 +++++- examples/basic-server-vanillajs/server-utils.ts | 6 +++++- examples/basic-server-vue/server-utils.ts | 6 +++++- examples/budget-allocator-server/server-utils.ts | 6 +++++- examples/cohort-heatmap-server/server-utils.ts | 6 +++++- examples/customer-segmentation-server/server-utils.ts | 6 +++++- examples/integration-server/server-utils.ts | 6 +++++- examples/scenario-modeler-server/server-utils.ts | 6 +++++- examples/sheet-music-server/server-utils.ts | 6 +++++- examples/system-monitor-server/server-utils.ts | 6 +++++- examples/threejs-server/server-utils.ts | 6 +++++- examples/video-resource-server/server-utils.ts | 6 +++++- examples/wiki-explorer-server/server-utils.ts | 6 +++++- 16 files changed, 80 insertions(+), 16 deletions(-) diff --git a/examples/basic-server-preact/server-utils.ts b/examples/basic-server-preact/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/basic-server-preact/server-utils.ts +++ b/examples/basic-server-preact/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/basic-server-react/server-utils.ts b/examples/basic-server-react/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/basic-server-react/server-utils.ts +++ b/examples/basic-server-react/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/basic-server-solid/server-utils.ts b/examples/basic-server-solid/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/basic-server-solid/server-utils.ts +++ b/examples/basic-server-solid/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/basic-server-svelte/server-utils.ts b/examples/basic-server-svelte/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/basic-server-svelte/server-utils.ts +++ b/examples/basic-server-svelte/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/basic-server-vanillajs/server-utils.ts b/examples/basic-server-vanillajs/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/basic-server-vanillajs/server-utils.ts +++ b/examples/basic-server-vanillajs/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/basic-server-vue/server-utils.ts b/examples/basic-server-vue/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/basic-server-vue/server-utils.ts +++ b/examples/basic-server-vue/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/budget-allocator-server/server-utils.ts b/examples/budget-allocator-server/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/budget-allocator-server/server-utils.ts +++ b/examples/budget-allocator-server/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/cohort-heatmap-server/server-utils.ts b/examples/cohort-heatmap-server/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/cohort-heatmap-server/server-utils.ts +++ b/examples/cohort-heatmap-server/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/customer-segmentation-server/server-utils.ts b/examples/customer-segmentation-server/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/customer-segmentation-server/server-utils.ts +++ b/examples/customer-segmentation-server/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/integration-server/server-utils.ts b/examples/integration-server/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/integration-server/server-utils.ts +++ b/examples/integration-server/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/scenario-modeler-server/server-utils.ts b/examples/scenario-modeler-server/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/scenario-modeler-server/server-utils.ts +++ b/examples/scenario-modeler-server/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/sheet-music-server/server-utils.ts b/examples/sheet-music-server/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/sheet-music-server/server-utils.ts +++ b/examples/sheet-music-server/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/system-monitor-server/server-utils.ts b/examples/system-monitor-server/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/system-monitor-server/server-utils.ts +++ b/examples/system-monitor-server/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/threejs-server/server-utils.ts b/examples/threejs-server/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/threejs-server/server-utils.ts +++ b/examples/threejs-server/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/video-resource-server/server-utils.ts b/examples/video-resource-server/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/video-resource-server/server-utils.ts +++ b/examples/video-resource-server/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); }); diff --git a/examples/wiki-explorer-server/server-utils.ts b/examples/wiki-explorer-server/server-utils.ts index c700c818..9fe9745a 100644 --- a/examples/wiki-explorer-server/server-utils.ts +++ b/examples/wiki-explorer-server/server-utils.ts @@ -54,7 +54,11 @@ export async function startServer( } }); - const httpServer = app.listen(port, () => { + const httpServer = app.listen(port, (err) => { + if (err) { + console.error("Failed to start server:", err); + process.exit(1); + } console.log(`${name} listening on http://localhost:${port}/mcp`); });