fish2018/pansou · error

加载配置失败

Error message

加载配置失败: %w

What it means

PanlianPlugin.Initialize calls p.loadConfig() after creating the storage directory; a failure is wrapped as '加载配置失败' ('failed to load config'). It means the plugin's persisted configuration (file in the storage dir) could not be read or parsed, so initialization is aborted.

Solutions

  1. Inspect and fix or delete the corrupt config file in <cachePath>/panlian_users so the plugin can regenerate defaults.
  2. Check file permissions on the storage dir and config file.
  3. Validate the config JSON syntax (jq . <configfile>).
  4. Re-initialize the plugin with a fresh cache directory after backing up user data.

Example fix

// before
$ cat cache/panlian_users/config.json  // trailing comma -> parse error
// after
$ mv cache/panlian_users/config.json config.json.bak
$ # restart; plugin recreates default config
Defensive patterns

Strategy: fallback

Validate before calling

// before Initialize, sanity-check the persisted config
if data, err := os.ReadFile(cfgPath); err == nil {
    var v map[string]any
    if err := json.Unmarshal(data, &v); err != nil {
        os.Rename(cfgPath, cfgPath+".corrupt") // let plugin regenerate
    }
}

Try / catch

if err := plugin.Initialize(cachePath, cfg); err != nil {
    if strings.Contains(err.Error(), "加载配置失败") {
        resetPluginConfig() // delete/regenerate config then retry once
        return plugin.Initialize(cachePath, cfg)
    }
    return err
}

Prevention

When it happens

Trigger: loadConfig fails when the config file under storageDir (panlian_users dir) is missing/corrupt/unreadable, or contains invalid JSON, typically right after a failed mkdir or a partially-written config from a previous crash.

Common situations: Config file truncated by an unclean shutdown; permissions changed on the cache dir; config JSON hand-edited with a syntax error; storage dir wiped but expected fields absent and loader requires them.

Understand the failure class

Background: "Config file not found": what it means and how to fix it in docker-sync, Maven, Vagrant, Turborepo and other tools — this error's family across 60 libraries.

Related errors


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

Appendix: source

Thrown at plugin/panlian/panlian.go:535

	return Description
}

func (p *PanlianPlugin) Initialize() error {
	if p.initialized {
		return nil
	}

	cachePath := os.Getenv("CACHE_PATH")
	if cachePath == "" {
		cachePath = "./cache"
	}
	storageDir = filepath.Join(cachePath, "panlian_users")

	if err := os.MkdirAll(storageDir, 0o755); err != nil {
		return fmt.Errorf("创建存储目录失败: %w", err)
	}
	if err := p.loadConfig(); err != nil {
		return fmt.Errorf("加载配置失败: %w", err)
	}
	p.loadAllUsers()
	p.initialized = true
	return nil
}

func (p *PanlianPlugin) RegisterWebRoutes(router *gin.RouterGroup) {
	group := router.Group("/panlian")
	group.GET("/:param", p.handleManagePage)
	group.POST("/:param", p.handleManagePagePOST)
}

func (p *PanlianPlugin) Search(keyword string, ext map[string]interface{}) ([]model.SearchResult, error) {
	result, err := p.SearchWithResult(keyword, ext)
	if err != nil {
		return nil, err
	}
	return result.Results, nil

View on GitHub (pinned to beaa561337)