Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func (c *Config) UpdateCache(response map[string]interface{}) interface{} {
apiVerbMap = nil

count := response["count"]
if response["api"] == nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This nil check stops the original panic, but a few things are still off:

  1. The error goes to stdout, not stderr.
  2. The message hard-codes "sync", but UpdateCache is also called from LoadCache.
  3. apiCache/apiVerbMap are already cleared before the early return, so a transient bad response wipes a cache that was previously fine.
  4. If response["api"] is non-nil but not a []interface{}, the type assertion still panics.

A safe type assertion placed before the reset covers all four. Returning 0 instead of nil also keeps sync from printing Discovered APIs. Suggested top of the function (note the two reset lines move down):

func (c *Config) UpdateCache(response map[string]interface{}) interface{} {
	apiList, valid := response["api"].([]interface{})
	if !valid || len(apiList) == 0 {
		fmt.Fprintln(os.Stderr, "Error: no APIs found in the discovery response, keeping the existing API cache. Please run 'sync'.")
		return 0
	}

	apiCache = make(map[string]*API)
	apiVerbMap = nil

	count := response["count"]

	for _, node := range apiList {

os is already imported in this file, so no import change is needed.

Tested locally with a cache file of {"count":0}, {"count":1,"api":"boom"} and {"count":0,"api":[]} — all three print the error to stderr instead of panicking, and go vet ./config/ is clean.

fmt.Println("Error: empty API list received, sync failed")
return nil
}
Comment on lines 114 to +118
apiList := response["api"].([]interface{})

for _, node := range apiList {
Expand Down