datawhalechina/hello-agents · error

消息发送失败

Error message

消息发送失败

What it means

Application-logic guard for POST /api/game/chat: HTTP was 2xx and JSON parsed, but result.success is falsy. Throws the backend's result.error, falling back to '消息发送失败' when the backend omits the error field. Means the chat turn was rejected at the business layer — most commonly an invalid/expired session_id or game already over.

Source

Thrown at Co-creation-projects/afei-GuessWhoAmI/frontend/app.js:117

      return;
    }

    input.value = '';
    this.addMessage(message, 'user');
    this.setControlsDisabled(true);

    try {
      const response = await fetch('http://localhost:8000/api/game/chat', {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({session_id: this.sessionId, message: message})
      });

      if (!response.ok) throw new Error(`HTTP error: ${response.status}`);

      const result = await response.json();

      if (!result.success) throw new Error(result.error || '消息发送失败');

      const data = result.data;
      this.addMessage(data.response, 'agent');

      // Update remaining questions from server
      this.remainingQuestions = data.remaining_questions;
      this.updateStats();

      if (data.is_game_over) {
        this.endGame(false);
      }

    } catch (error) {
      this.addMessage(`⚠️ 发送失败:${error.message}`, 'agent');
      console.error('Send message error:', error);
    } finally {
      this.setControlsDisabled(false);
    }

View on GitHub (pinned to 606a07d341)

Solutions

  1. Log result on failure and fix the backend to always include a descriptive error string
  2. On session-invalid errors, restart the game automatically
  3. Align frontend fallback text with likely causes and surface result for support
  4. Add backend tests asserting error is present whenever success is false
Defensive patterns

Strategy: validation

Validate before calling

if (!this.sessionId) { await this.startNewGame(); }

Type guard

function isChatResult(r) { return r && typeof r.success === 'boolean' && (!r.success || ('data' in r && 'response' in r.data)); }

Try / catch

try { ... } catch (e) { alert(`消息发送失败:${e.message}`); this.setControlsDisabled(false); }

Prevention

When it happens

Trigger: Backend returns success:false with no error field for conditions like unknown session_id, game finished, question budget exhausted, or message content rejected. The literal fallback masks the actual reason.

Common situations: Session expired server-side while the tab stayed open; backend developer forgot to set error when returning success:false; envelope drift after refactor.

Related errors


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/e482f9c04f840b19. Report an issue: GitHub.