flipped-aurora/gin-vue-admin · error · Error

LLM request failed

Error message

LLM request failed

What it means

streamLLMRequest first tries to consume the response as an SSE stream; if the Content-Type is not text/event-stream it falls back to normal JSON parsing. If the JSON body has a non-zero code, it throws 'LLM request failed' with the server's msg if present.

Source

Thrown at web/src/api/autoCode.js:167

    })

    if (!response.ok) {
      throw await buildFetchError(response)
    }

    const contentType = String(
      response.headers.get('content-type') || ''
    ).toLowerCase()
    // 上游非 SSE:降级为普通 JSON 解析
    if (!contentType.includes('text/event-stream') || !response.body) {
      console.warn(
        '[SSE] 响应非 SSE 格式,Content-Type:',
        contentType,
        '| 降级为普通 JSON 解析'
      )
      const body = await parseFetchBody(response)
      if (typeof body?.code !== 'undefined' && body.code !== 0) {
        throw new Error(body.msg || 'LLM request failed')
      }
      return body
    }
    console.debug('[SSE] 已进入流式读取模式')

    const reader = response.body.getReader()
    const decoder = new TextDecoder()
    let buffer = ''
    let done = false

    while (!done) {
      const result = await reader.read()
      done = result.done
      if (done) break

      buffer += decoder.decode(result.value, { stream: true })
      const lines = buffer.split('\n')
      // 保留最后一条可能不完整的行

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Inspect body.msg from the response to find the real server-side error (often auth or permission)
  2. Confirm the request URL targets the LLM SSE endpoint and the user has API/casbin permission for it
  3. Check server logs at the corresponding handler for the failure reason
  4. Ensure proxies/nginx pass through the text/event-stream Content-Type
Defensive patterns

Strategy: validation

Validate before calling

const ct = response.headers.get('Content-Type') || ''
if (!ct.includes('text/event-stream') && !ct.includes('application/json')) {
  throw new Error(`Unexpected content-type: ${ct}`)
}

Type guard

function isApiErrorBody(body) {
  return typeof body?.code !== 'undefined' && body.code !== 0
}

Try / catch

try {
  const body = await streamLLMRequest(url, options)
} catch (e) {
  if (e.message === 'LLM request failed') {
    ElMessage.error('请求被服务端拒绝,请检查登录态与权限')
  }
}

Prevention

When it happens

Trigger: The fetch response Content-Type is not SSE, the body parses as JSON with body.code !== 0, and body.msg is empty/undefined so the fallback message is thrown.

Common situations: Backend returns a standard {code,msg} error envelope (auth failure, Casbin denial, validation error) instead of an SSE stream; wrong route hit returning JSON; proxy strips the SSE Content-Type.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/d4af79e27f70e8d6. Report an issue: GitHub.