Improve startup errors with actionable port conflict guidance
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
e6540e7f2d
commit
ebefed3567
2 files changed
+42
-4
No files matched your search
+21
-1
@@ -3,7 +3,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseCliOptions, readCliVersion } from "./cli";
|
||||
import { formatStartupError, parseCliOptions, readCliVersion } from "./cli";
|
||||
|
||||
describe("parseCliOptions", () => {
|
||||
it("reads defaults from environment variables", () => {
|
||||
@@ -87,3 +87,23 @@ describe("readCliVersion", () => {
|
||||
expect(value).toBe("0.1.0");
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatStartupError", () => {
|
||||
const options = parseCliOptions([], {}, "/workspace");
|
||||
|
||||
it("returns helpful guidance for port conflicts", () => {
|
||||
const message = formatStartupError({ code: "EADDRINUSE" }, options);
|
||||
expect(message).toContain("Port already in use");
|
||||
expect(message).toContain("--backend-port");
|
||||
});
|
||||
|
||||
it("returns error message when available", () => {
|
||||
const message = formatStartupError(new Error("boom"), options);
|
||||
expect(message).toBe("boom");
|
||||
});
|
||||
|
||||
it("falls back to generic startup error text", () => {
|
||||
const message = formatStartupError({}, options);
|
||||
expect(message).toBe("Failed to start t3 runtime.");
|
||||
});
|
||||
});
|
||||
+21
-3
@@ -42,6 +42,11 @@ interface CliOptions {
|
||||
showVersion: boolean;
|
||||
}
|
||||
|
||||
interface StartupErrorShape {
|
||||
code?: unknown;
|
||||
message?: unknown;
|
||||
}
|
||||
|
||||
function readArgValue(args: string[], index: number, key: string): string {
|
||||
const value = args[index + 1];
|
||||
if (!value || value.startsWith("-")) {
|
||||
@@ -51,6 +56,21 @@ function readArgValue(args: string[], index: number, key: string): string {
|
||||
return value;
|
||||
}
|
||||
|
||||
export function formatStartupError(error: unknown, options: CliOptions): string {
|
||||
const candidate = error as StartupErrorShape;
|
||||
const code = typeof candidate?.code === "string" ? candidate.code : undefined;
|
||||
|
||||
if (code === "EADDRINUSE") {
|
||||
return `Port already in use. Try --backend-port ${options.backendPort + 1} --web-port ${options.webPort + 1} or stop the conflicting process.`;
|
||||
}
|
||||
|
||||
if (error instanceof Error && error.message.length > 0) {
|
||||
return error.message;
|
||||
}
|
||||
|
||||
return "Failed to start t3 runtime.";
|
||||
}
|
||||
|
||||
export function parseCliOptions(
|
||||
argv: string[],
|
||||
env: NodeJS.ProcessEnv,
|
||||
@@ -349,9 +369,7 @@ async function main() {
|
||||
try {
|
||||
await runCli(options);
|
||||
} catch (error) {
|
||||
process.stderr.write(
|
||||
`${error instanceof Error ? error.message : "Failed to start t3 runtime."}\n`,
|
||||
);
|
||||
process.stderr.write(`${formatStartupError(error, options)}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user