-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
88 lines (86 loc) · 2.05 KB
/
errors_test.go
File metadata and controls
88 lines (86 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package openai
import (
"errors"
"io"
"net/http"
"strings"
"testing"
)
func TestCheckErrResponse(t *testing.T) {
t.Parallel()
t.Run("status code is 200", func(t *testing.T) {
t.Parallel()
resp := &http.Response{
StatusCode: 200,
}
err := checkErrResponse(resp)
if err != nil {
t.Errorf("expected no error but got %v", err)
}
})
t.Run("status code is 400", func(t *testing.T) {
t.Parallel()
resp := &http.Response{
StatusCode: 400,
}
err := checkErrResponse(resp)
if err == nil {
t.Error("expected error but got nil")
}
})
t.Run("text content-type returns simple error", func(t *testing.T) {
t.Parallel()
resp := &http.Response{
StatusCode: 400,
Header: http.Header{
"Content-Type": []string{"text/plain"},
},
}
err := checkErrResponse(resp)
if err == nil {
t.Error("expected error but got nil")
}
})
t.Run("json content-type and openAI error json return OpenAIAPIErrorDetails error", func(t *testing.T) {
t.Parallel()
resp := &http.Response{
StatusCode: 400,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Body: io.NopCloser(strings.NewReader(`{
"error": {
"code": "invalid_api_key",
"message": "Incorrect API key provided...",
"param": null,
"type": "invalid_request_error"
}
}`)),
}
err := checkErrResponse(resp)
var openAPIErr *APIError
if ok := errors.As(err, &openAPIErr); !ok {
t.Errorf("expected OpenAI error error but got: %#v", err)
}
})
t.Run(`json content-type and openAI error json missing "message" returns ErrDecodingResponse error`, func(t *testing.T) {
t.Parallel()
resp := &http.Response{
StatusCode: 400,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Body: io.NopCloser(strings.NewReader(`{
"error": {
"code": "invalid_api_key",
"param": null,
"type": "invalid_request_error"
}
}`)),
}
err := checkErrResponse(resp)
if !errors.Is(err, ErrDecodingResponse) {
t.Errorf("expected ErrDecodingResponse error but got: %#v", err)
}
})
}