Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/auth/refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ export async function refreshToken(
});

if (!res.ok) {
const body = await res.text().catch(() => '');
throw new CLIError(
`Token refresh failed: ${res.status}`,
`Token refresh failed: ${res.status} ${body}`.trim(),
ExitCode.AUTH,
'Run `polylane auth login` to re-authenticate'
);
Expand Down
8 changes: 6 additions & 2 deletions src/auth/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ export async function resolveCredential(config: Config): Promise<Credential> {
if (isTokenExpiringSoon(stored)) {
try {
return await refreshToken(config, stored);
} catch {
// Fall through
} catch (err) {
if (config.verbose) {
const msg = err instanceof Error ? err.message : String(err);
process.stderr.write(`Token refresh failed: ${msg}
`);
}
}
} else {
return stored;
Expand Down
10 changes: 7 additions & 3 deletions src/client/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,16 @@ export async function requestJson<T>(config: Config, opts: RequestOpts): Promise
}

let json: ApiEnvelope<T>;
let bodyText = '';
try {
json = (await res.json()) as ApiEnvelope<T>;
bodyText = await res.text();
json = JSON.parse(bodyText) as ApiEnvelope<T>;
} catch {
const snippet = bodyText.slice(0, 500);
throw new CLIError(
`Invalid JSON response from server (status ${res.status})`,
ExitCode.GENERAL
`Invalid JSON response from ${opts.method ?? 'GET'} ${opts.url} (status ${res.status}): ${snippet}`,
ExitCode.GENERAL,
`Check --domain=${config.domain} and your network`
);
}

Expand Down
8 changes: 6 additions & 2 deletions src/client/thread-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,16 @@ export async function sendThreadMessage(
rejectOnce(new CLIError(`WebSocket error: ${err.message}`, ExitCode.NETWORK));
});

ws.on('close', () => {
ws.on('close', (code: number, reason: Buffer) => {
if (!resolved) {
if (blockOrder.length > 0) {
finalize();
} else {
rejectOnce(new CLIError('WebSocket closed before reply', ExitCode.NETWORK));
const reasonText = reason.toString('utf-8').trim();
rejectOnce(new CLIError(
`WebSocket closed before reply (code ${code}${reasonText ? `, ${reasonText}` : ''})`,
ExitCode.NETWORK
));
}
}
});
Expand Down
Loading