fix: status command shows detailed API health (rate-limit, auth, unreachable)

checkHealth() now returns 'ok' | 'rate_limited' | 'auth_error' | 'unreachable'
instead of boolean, so /status can display the specific issue with color-coded
indicators instead of just "Nicht erreichbar" for all failures.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Martin Porwoll 2026-03-01 10:29:57 +00:00
parent a2ee6d8cbe
commit 11e91a26c1
2 changed files with 17 additions and 16 deletions

View file

@ -168,13 +168,18 @@ class PayloadClient {
return data.docs;
}
async checkHealth(): Promise<boolean> {
async checkHealth(): Promise<'ok' | 'rate_limited' | 'auth_error' | 'unreachable'> {
try {
const response = await this.authFetch(`${this.apiUrl}/users/me`);
return response.ok;
if (response.ok) return 'ok';
if (response.status === 401) return 'auth_error';
return 'unreachable';
} catch (error) {
const msg = error instanceof Error ? error.message : '';
log.error('Health check failed', error);
return false;
if (msg.includes('429') || msg.includes('RATE_LIMITED')) return 'rate_limited';
if (msg.includes('401') || msg.includes('incorrect')) return 'auth_error';
return 'unreachable';
}
}

View file

@ -384,18 +384,14 @@ export function registerHandlers(bot: Bot<BotContext>): void {
// /status command
bot.command('status', async (ctx) => {
let apiStatus: string;
try {
const healthy = await payloadClient.checkHealth();
apiStatus = healthy ? 'Erreichbar' : 'Nicht erreichbar';
} catch (error) {
const msg = error instanceof Error ? error.message : '';
if (msg.includes('429') || msg.includes('RATE_LIMITED')) {
apiStatus = 'Rate\\-Limit \\(bitte warten\\)';
} else {
apiStatus = 'Nicht erreichbar';
}
}
const health = await payloadClient.checkHealth();
const apiLabels: Record<string, string> = {
ok: '\ud83d\udfe2 Erreichbar',
rate_limited: '\ud83d\udfe1 Rate\\-Limit \\(bitte warten\\)',
auth_error: '\ud83d\udd34 Anmeldefehler',
unreachable: '\ud83d\udd34 Nicht erreichbar',
};
const apiStatus = apiLabels[health] ?? '\ud83d\udd34 Unbekannt';
const expiry = payloadClient.getTokenExpiry();
const expiryStr = expiry ? expiry.toLocaleString('de-DE') : 'Kein Token';
@ -407,7 +403,7 @@ export function registerHandlers(bot: Bot<BotContext>): void {
const text =
`\ud83d\udcca *Bot Status*\n\n` +
`\u23f1 Uptime: ${escapeMarkdown(formatUptime())}\n` +
`\ud83d\udfe2 API: ${apiStatus}\n` +
`${apiStatus}\n` +
`\ud83c\udfe2 Tenant: ${tenantStr}\n` +
`\ud83d\udd11 Token läuft ab: ${escapeMarkdown(expiryStr)}`;