Skip to content

Conversation

Copy link

Copilot AI commented Jan 5, 2026

Tianyancha API queries panic with "invalid memory address or nil pointer dereference" when cookie authentication fails or page structure changes.

Changes

  • Added nil check in searchBaseInfo: Guard against missing __NEXT_DATA__ HTML element before calling htmlquery.InnerText()
  • Added empty array validation: Check enInfoD length to prevent index out of bounds access
  • Improved error messages: Return descriptive errors directing users to verify cookie configuration

Technical Details

When htmlquery.FindOne() fails to locate the __NEXT_DATA__ script tag (typically due to invalid/expired cookies), it returns nil. The previous code immediately dereferenced this nil pointer:

htmlInfos := htmlquery.FindOne(body, "//*[@id=\"__NEXT_DATA__\"]")
enInfo := gjson.Parse(htmlquery.InnerText(htmlInfos))  // panic if htmlInfos is nil

Now validates before dereferencing:

htmlInfos := htmlquery.FindOne(body, "//*[@id=\"__NEXT_DATA__\"]")
if htmlInfos == nil {
    gologger.Error().Msgf("【TYC】COOKIE检查失效,请检查COOKIE是否正确!\n")
    return gjson.Result{}, gjson.Result{}
}
Original prompt

This section details on the original issue you should resolve

<issue_title>使用天眼查API查询报错</issue_title>
<issue_description>使用2.0.1版本进行天眼查API查询

Image

执行后报错

Image

天眼查部分仅配置天眼查API-KEY

Image

使用1.3.1版本执行天眼查API查询同样报错

Image</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix issue with Tianyancha API query error Fix nil pointer panic in TYC searchBaseInfo when HTML element missing Jan 5, 2026
Copilot AI requested a review from keac January 5, 2026 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

使用天眼查API查询报错

2 participants