Skip to content

Commit 9571b69

Browse files
committed
Fix: Improve error handling for config loading
- Updates unmarshal errors in store functions to handle - old config formats, providing user instructions to - delete and re-run setup for better usability.
1 parent ec65aa7 commit 9571b69

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

cmd/cli/store/store.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type StoreMethods struct {
1818
ring keyring.Keyring
1919
}
2020

21-
//Initializes Keyring instance
21+
// Initializes Keyring instance
2222
func KeyringInit() (*StoreMethods, error) {
2323
ring, err := keyring.Open(keyring.Config{
2424
ServiceName: "commit-msg",
@@ -69,7 +69,9 @@ func (s *StoreMethods) Save(LLMConfig LLMProvider) error {
6969
if len(data) > 2 {
7070
err = json.Unmarshal(data, &cfg)
7171
if err != nil {
72-
return err
72+
// If unmarshal fails, it might be due to old config format
73+
// Reset to empty config to allow fresh setup
74+
return fmt.Errorf("config file format error: %w. Please delete the config and run setup again", err)
7375
}
7476
}
7577

@@ -135,7 +137,8 @@ func (s *StoreMethods) DefaultLLMKey() (*LLMProvider, error) {
135137
if len(data) > 2 {
136138
err = json.Unmarshal(data, &cfg)
137139
if err != nil {
138-
return nil, err
140+
// If unmarshal fails, it might be due to old config format
141+
return nil, fmt.Errorf("config file format error: %w. Please delete the config and run setup again", err)
139142
}
140143
} else {
141144
return nil, errors.New("config file is empty, Please add at least one LLM Key")
@@ -180,7 +183,8 @@ func ListSavedModels() (*Config, error) {
180183
if len(data) > 2 {
181184
err = json.Unmarshal(data, &cfg)
182185
if err != nil {
183-
return nil, err
186+
// If unmarshal fails, it might be due to old config format
187+
return nil, fmt.Errorf("config file format error: %w. Please delete the config and run setup again", err)
184188
}
185189
} else {
186190
return nil, errors.New("config file is empty, Please add at least one LLM Key")
@@ -213,7 +217,8 @@ func ChangeDefault(Model types.LLMProvider) error {
213217
if len(data) > 2 {
214218
err = json.Unmarshal(data, &cfg)
215219
if err != nil {
216-
return err
220+
// If unmarshal fails, it might be due to old config format
221+
return fmt.Errorf("config file format error: %w. Please delete the config and run setup again", err)
217222
}
218223
}
219224

@@ -262,7 +267,8 @@ func (s *StoreMethods) DeleteModel(Model types.LLMProvider) error {
262267
if len(data) > 2 {
263268
err = json.Unmarshal(data, &cfg)
264269
if err != nil {
265-
return err
270+
// If unmarshal fails, it might be due to old config format
271+
return fmt.Errorf("config file format error: %w. Please delete the config and run setup again", err)
266272
}
267273
}
268274

@@ -323,7 +329,8 @@ func (s *StoreMethods) UpdateAPIKey(Model types.LLMProvider, APIKey string) erro
323329
if len(data) > 2 {
324330
err = json.Unmarshal(data, &cfg)
325331
if err != nil {
326-
return err
332+
// If unmarshal fails, it might be due to old config format
333+
return fmt.Errorf("config file format error: %w. Please delete the config and run setup again", err)
327334
}
328335
}
329336

0 commit comments

Comments
 (0)