@commonlyai/cli 0.1.84 → 0.1.85
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/lib/api.js +22 -2
package/package.json
CHANGED
package/src/lib/api.js
CHANGED
|
@@ -20,14 +20,34 @@ export const sessionExpiredMessage = ({ instanceKey, baseUrl }) => (
|
|
|
20
20
|
`Session for ${instanceKey} (${baseUrl}) has expired.\nRun: commonly login --instance ${instanceKey}`
|
|
21
21
|
);
|
|
22
22
|
|
|
23
|
+
// A non-JSON error body is usually an edge proxy's HTML page; a Cloudflare
|
|
24
|
+
// tunnel error is ~120 lines, and the message used to be all of them, so every
|
|
25
|
+
// failed poll dumped a whole page into the agent log. The message names the
|
|
26
|
+
// page by its <title>; err.body keeps the full text for callers that classify it.
|
|
27
|
+
const MAX_TEXT_ERROR = 300;
|
|
28
|
+
const summarizeTextError = (text, status) => {
|
|
29
|
+
const trimmed = String(text || '').trim();
|
|
30
|
+
if (!trimmed) return null;
|
|
31
|
+
if (/^<(?:!doctype|html)/i.test(trimmed) || /<html[\s>]/i.test(trimmed)) {
|
|
32
|
+
const title = trimmed.match(/<title[^>]*>([\s\S]*?)<\/title>/i)?.[1]?.replace(/\s+/g, ' ').trim();
|
|
33
|
+
return title ? `HTTP ${status}: ${title}` : `HTTP ${status} (HTML error page)`;
|
|
34
|
+
}
|
|
35
|
+
const line = trimmed.replace(/\s+/g, ' ');
|
|
36
|
+
return line.length > MAX_TEXT_ERROR ? `${line.slice(0, MAX_TEXT_ERROR)}…` : line;
|
|
37
|
+
};
|
|
38
|
+
|
|
23
39
|
const handleResponse = async (res, session = null) => {
|
|
24
40
|
const text = await res.text();
|
|
25
41
|
let body;
|
|
26
|
-
|
|
42
|
+
let isJson = true;
|
|
43
|
+
try { body = JSON.parse(text); } catch { body = { message: text }; isJson = false; }
|
|
27
44
|
if (!res.ok) {
|
|
45
|
+
const serverMessage = isJson
|
|
46
|
+
? body?.error || body?.message || body?.msg
|
|
47
|
+
: summarizeTextError(text, res.status);
|
|
28
48
|
const msg = res.status === 401 && session && knownSessionFailure(body)
|
|
29
49
|
? sessionExpiredMessage(session)
|
|
30
|
-
:
|
|
50
|
+
: serverMessage || `HTTP ${res.status}`;
|
|
31
51
|
const err = new Error(msg);
|
|
32
52
|
err.status = res.status;
|
|
33
53
|
err.body = body;
|