Skip to content

Commit 923faca

Browse files
committed
chore(llm): add custom error types for OpenAI integration
- Create APIKeyMissingError for missing API keys - Implement OpenAIRequestError for OpenAI request failures - Add JSONParseError for JSON parsing issues - Update error handling in openai.go to use custom error types [Therapy notes by Kommitment - github.com/cowboy-bebug/kommitment]
1 parent 4929e0b commit 923faca

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

internal/llm/errors.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package llm
2+
3+
import "fmt"
4+
5+
type APIKeyMissingError struct{}
6+
type OpenAIRequestError struct{ Err error }
7+
type JSONParseError struct{ Err error }
8+
9+
func (e APIKeyMissingError) Error() string {
10+
return "KOMMIT_API_KEY or OPENAI_API_KEY environment variable must be set"
11+
}
12+
13+
func (e OpenAIRequestError) Error() string {
14+
return fmt.Sprintf("OpenAI request failed: %v", e.Err)
15+
}
16+
17+
func (e JSONParseError) Error() string {
18+
return fmt.Sprintf("JSON unmarshal failed: %v", e.Err)
19+
}

internal/llm/openai.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func newClient() (*openai.Client, error) {
3131

3232
// If both are missing, return an error
3333
if apiKey == "" {
34-
return nil, fmt.Errorf("KOMMIT_API_KEY or OPENAI_API_KEY environment variable must be set")
34+
return nil, &APIKeyMissingError{}
3535
}
3636

3737
return openai.NewClient(
@@ -43,7 +43,7 @@ func newClient() (*openai.Client, error) {
4343
func chat(model, prompt string) (string, error) {
4444
client, err := newClient()
4545
if err != nil {
46-
return "", fmt.Errorf("error creating OpenAI client: %v", err)
46+
return "", err
4747
}
4848

4949
resp, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
@@ -58,7 +58,7 @@ func chat(model, prompt string) (string, error) {
5858
FrequencyPenalty: openai.Float(frequencyPenalty),
5959
})
6060
if err != nil {
61-
return "", fmt.Errorf("OpenAI request failed: %v", err)
61+
return "", &OpenAIRequestError{Err: err}
6262
}
6363

6464
return resp.Choices[0].Message.Content, nil
@@ -78,7 +78,7 @@ func chatStructured[T any](model, prompt string, schema openai.ResponseFormatJSO
7878
client, err := newClient()
7979
if err != nil {
8080
var empty T
81-
return empty, fmt.Errorf("error creating OpenAI client: %v", err)
81+
return empty, err
8282
}
8383

8484
resp, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
@@ -99,14 +99,14 @@ func chatStructured[T any](model, prompt string, schema openai.ResponseFormatJSO
9999
})
100100
if err != nil {
101101
var empty T
102-
return empty, fmt.Errorf("OpenAI request failed: %v", err)
102+
return empty, &OpenAIRequestError{Err: err}
103103
}
104104

105105
content := resp.Choices[0].Message.Content
106106
var result T
107107
if err := json.Unmarshal([]byte(content), &result); err != nil {
108108
var empty T
109-
return empty, fmt.Errorf("failed to parse JSON response: %v", err)
109+
return empty, &JSONParseError{Err: err}
110110
}
111111

112112
return result, nil

0 commit comments

Comments
 (0)