fish2018/pansou · error

[ ] API返回错误

Error message

[%s] API返回错误: %s

What it means

Application-level error: the ouge API responded with valid JSON whose code field is not 1, so the plugin surfaces apiResponse.Msg as the failure reason. This is the upstream API explicitly reporting an error (auth failure, bad parameters, quota, internal error) rather than a transport problem.

Solutions

  1. Log apiResponse.Msg verbatim — it is the upstream-provided diagnosis
  2. Check whether the request parameters (keyword encoding, pagination) are accepted by the upstream API
  3. Verify any credentials/token the API expects are still valid
  4. Handle specific code values distinctly if the API documents them (rate limit vs auth vs server error)

Example fix

// before
if apiResponse.Code != 1 {
    return nil, fmt.Errorf("[%s] API返回错误: %s", p.Name(), apiResponse.Msg)
}
// after
if apiResponse.Code != 1 {
    if apiResponse.Code == 429 { return nil, errRateLimited }
    return nil, fmt.Errorf("[%s] API返回错误 code=%d: %s", p.Name(), apiResponse.Code, apiResponse.Msg)
}
Defensive patterns

Strategy: try-catch

Try / catch

if err != nil {
    if strings.Contains(err.Error(), "API返回错误") {
        // upstream explicitly refused: don't blind-retry, surface msg to user
        return fmt.Errorf("upstream rejected search: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: searchImpl gets a 200 with parsed JSON where apiResponse.Code != 1; the message text in apiResponse.Msg carries the upstream reason.

Common situations: API key/token expired server-side; search keyword rejected by upstream; upstream service degraded and returns code!=1 with an error message; undocumented code values added by an API version change.

Related errors


AI-assisted analysis of fish2018/pansou@beaa561337 (2026-09-07). Data as JSON: /api/errors/0d794fde7b58512b. Report an issue: GitHub.

Appendix: source

Thrown at plugin/ouge/ouge.go:159

	if err != nil {
		return nil, fmt.Errorf("[%s] 搜索请求失败: %w", p.Name(), err)
	}
	defer resp.Body.Close()
	
	// 解析JSON响应
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("[%s] 读取响应失败: %w", p.Name(), err)
	}
	
	var apiResponse OugeAPIResponse
	if err := json.Unmarshal(body, &apiResponse); err != nil {
		return nil, fmt.Errorf("[%s] 解析JSON响应失败: %w", p.Name(), err)
	}
	
	// 检查API响应状态
	if apiResponse.Code != 1 {
		return nil, fmt.Errorf("[%s] API返回错误: %s", p.Name(), apiResponse.Msg)
	}
	
	// 解析搜索结果
	var results []model.SearchResult
	for _, item := range apiResponse.List {
		if result := p.parseAPIItem(item); result.Title != "" {
			results = append(results, result)
		}
	}
	
	return results, nil
}

// OugeAPIResponse API响应结构
type OugeAPIResponse struct {
	Code      int           `json:"code"`
	Msg       string        `json:"msg"`
	Page      int           `json:"page"`

View on GitHub (pinned to beaa561337)