-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathhandler.ts
More file actions
124 lines (115 loc) · 4.34 KB
/
Copy pathhandler.ts
File metadata and controls
124 lines (115 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { CLIError } from "./base";
import { ExitCode } from "./codes";
import { detectOutputFormat } from "../output/formatter";
export function handleError(err: unknown): never {
if (err instanceof CLIError) {
const format = detectOutputFormat(process.env.MINIMAX_OUTPUT);
if (format === "json") {
process.stderr.write(JSON.stringify(err.toJSON(), null, 2) + "\n");
} else {
process.stderr.write(`\nError: ${err.message}\n`);
if (err.hint) {
process.stderr.write(`\n ${err.hint.split("\n").join("\n ")}\n`);
}
process.stderr.write(` (exit code ${err.exitCode})\n`);
}
process.exit(err.exitCode);
}
if (err instanceof Error) {
if (
err.name === "AbortError" ||
err.name === "TimeoutError" ||
err.message.includes("timed out")
) {
const timeout = new CLIError(
"Request timed out.",
ExitCode.TIMEOUT,
"Try increasing --timeout (e.g. --timeout 60).\n" +
"If this happens on every request with a valid API key, you may be hitting the wrong region.\n" +
"Run: mmx auth status — to check your credentials and region.\n" +
"Run: mmx config set region global (or cn) — to override the region.",
);
return handleError(timeout);
}
// Detect TypeError from fetch with invalid URL (e.g., malformed MINIMAX_BASE_URL)
if (err instanceof TypeError && err.message === "fetch failed") {
const networkErr = new CLIError(
"Network request failed.",
ExitCode.NETWORK,
"Check your network connection.\n" +
"To use a proxy: set HTTPS_PROXY env var, or run: mmx config set --key proxy --value http://HOST:PORT",
);
return handleError(networkErr);
}
// Detect network-level errors (proxy, connection refused, DNS, etc.)
const msg = err.message.toLowerCase();
const isNetworkError =
msg.includes("failed to fetch") ||
msg.includes("connection refused") ||
msg.includes("econnrefused") ||
msg.includes("connection reset") ||
msg.includes("econnreset") ||
msg.includes("network error") ||
msg.includes("enotfound") ||
msg.includes("getaddrinfo") ||
msg.includes("proxy") ||
msg.includes("socket") ||
msg.includes("etimedout") ||
msg.includes("timeout") ||
msg.includes("eai_AGAIN");
if (isNetworkError) {
let hint =
"Check your network connection.\n" +
"To use a proxy: set HTTPS_PROXY env var, or run: mmx config set --key proxy --value http://HOST:PORT";
if (msg.includes("proxy")) {
hint =
"Proxy connection failed — verify your proxy URL and authentication.\n" +
"Check: HTTPS_PROXY / HTTP_PROXY env vars, or mmx config show for configured proxy.";
}
const networkErr = new CLIError(
"Network request failed.",
ExitCode.NETWORK,
hint,
);
return handleError(networkErr);
}
// Detect filesystem-level errors (ENOENT, EACCES, ENOSPC, etc.)
const ecode = (err as NodeJS.ErrnoException).code;
if (
ecode === "ENOENT" ||
ecode === "EACCES" ||
ecode === "ENOSPC" ||
ecode === "ENOTDIR" ||
ecode === "EISDIR" ||
ecode === "EPERM" ||
ecode === "EBUSY"
) {
let hint = "Check the file path and permissions.";
if (ecode === "ENOENT") hint = "File or directory not found.";
if (ecode === "EACCES" || ecode === "EPERM")
hint = "Permission denied — check file or directory permissions.";
if (ecode === "ENOSPC") hint = "Disk full — free up space and try again.";
const fsErr = new CLIError(
`File system error: ${err.message}`,
ExitCode.GENERAL,
hint,
);
return handleError(fsErr);
} else if (typeof ecode === "string" && ecode.startsWith("E")) {
// All other Node.js filesystem error codes (EMFILE, EEXIST, EROFS, etc.)
const fsErr = new CLIError(
`File system error: ${err.message}`,
ExitCode.GENERAL,
"Check the file path and permissions.",
);
return handleError(fsErr);
}
process.stderr.write(`\nError: ${err.message}\n`);
if (process.env.MINIMAX_VERBOSE === "1") {
process.stderr.write(`${err.stack}\n`);
}
} else {
process.stderr.write(`\nError: ${String(err)}\n`);
}
process.exit(ExitCode.GENERAL);
}