Skip to content

Commit d5a37a1

Browse files
ochafikclaude
andcommitted
fix: add error handling for server startup failures
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 <noreply@anthropic.com>
1 parent df49e88 commit d5a37a1

16 files changed

Lines changed: 80 additions & 16 deletions

File tree

examples/basic-server-preact/server-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ export async function startServer(
5454
}
5555
});
5656

57-
const httpServer = app.listen(port, () => {
57+
const httpServer = app.listen(port, (err) => {
58+
if (err) {
59+
console.error("Failed to start server:", err);
60+
process.exit(1);
61+
}
5862
console.log(`${name} listening on http://localhost:${port}/mcp`);
5963
});
6064

examples/basic-server-react/server-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ export async function startServer(
5454
}
5555
});
5656

57-
const httpServer = app.listen(port, () => {
57+
const httpServer = app.listen(port, (err) => {
58+
if (err) {
59+
console.error("Failed to start server:", err);
60+
process.exit(1);
61+
}
5862
console.log(`${name} listening on http://localhost:${port}/mcp`);
5963
});
6064

examples/basic-server-solid/server-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ export async function startServer(
5454
}
5555
});
5656

57-
const httpServer = app.listen(port, () => {
57+
const httpServer = app.listen(port, (err) => {
58+
if (err) {
59+
console.error("Failed to start server:", err);
60+
process.exit(1);
61+
}
5862
console.log(`${name} listening on http://localhost:${port}/mcp`);
5963
});
6064

examples/basic-server-svelte/server-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ export async function startServer(
5454
}
5555
});
5656

57-
const httpServer = app.listen(port, () => {
57+
const httpServer = app.listen(port, (err) => {
58+
if (err) {
59+
console.error("Failed to start server:", err);
60+
process.exit(1);
61+
}
5862
console.log(`${name} listening on http://localhost:${port}/mcp`);
5963
});
6064

examples/basic-server-vanillajs/server-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ export async function startServer(
5454
}
5555
});
5656

57-
const httpServer = app.listen(port, () => {
57+
const httpServer = app.listen(port, (err) => {
58+
if (err) {
59+
console.error("Failed to start server:", err);
60+
process.exit(1);
61+
}
5862
console.log(`${name} listening on http://localhost:${port}/mcp`);
5963
});
6064

examples/basic-server-vue/server-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ export async function startServer(
5454
}
5555
});
5656

57-
const httpServer = app.listen(port, () => {
57+
const httpServer = app.listen(port, (err) => {
58+
if (err) {
59+
console.error("Failed to start server:", err);
60+
process.exit(1);
61+
}
5862
console.log(`${name} listening on http://localhost:${port}/mcp`);
5963
});
6064

examples/budget-allocator-server/server-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ export async function startServer(
5454
}
5555
});
5656

57-
const httpServer = app.listen(port, () => {
57+
const httpServer = app.listen(port, (err) => {
58+
if (err) {
59+
console.error("Failed to start server:", err);
60+
process.exit(1);
61+
}
5862
console.log(`${name} listening on http://localhost:${port}/mcp`);
5963
});
6064

examples/cohort-heatmap-server/server-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ export async function startServer(
5454
}
5555
});
5656

57-
const httpServer = app.listen(port, () => {
57+
const httpServer = app.listen(port, (err) => {
58+
if (err) {
59+
console.error("Failed to start server:", err);
60+
process.exit(1);
61+
}
5862
console.log(`${name} listening on http://localhost:${port}/mcp`);
5963
});
6064

examples/customer-segmentation-server/server-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ export async function startServer(
5454
}
5555
});
5656

57-
const httpServer = app.listen(port, () => {
57+
const httpServer = app.listen(port, (err) => {
58+
if (err) {
59+
console.error("Failed to start server:", err);
60+
process.exit(1);
61+
}
5862
console.log(`${name} listening on http://localhost:${port}/mcp`);
5963
});
6064

examples/integration-server/server-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ export async function startServer(
5454
}
5555
});
5656

57-
const httpServer = app.listen(port, () => {
57+
const httpServer = app.listen(port, (err) => {
58+
if (err) {
59+
console.error("Failed to start server:", err);
60+
process.exit(1);
61+
}
5862
console.log(`${name} listening on http://localhost:${port}/mcp`);
5963
});
6064

0 commit comments

Comments
 (0)