@@ -81,6 +81,53 @@ func TestAPI(t *testing.T) {
81
81
}
82
82
}
83
83
84
+ func TestAPIError (t * testing.T ) {
85
+ apiToken := os .Getenv ("OPENAI_TOKEN" )
86
+ if apiToken == "" {
87
+ t .Skip ("Skipping testing against production OpenAI API. Set OPENAI_TOKEN environment variable to enable it." )
88
+ }
89
+
90
+ var err error
91
+ c := NewClient (apiToken + "_invalid" )
92
+ ctx := context .Background ()
93
+ _ , err = c .ListEngines (ctx )
94
+ if err == nil {
95
+ t .Fatal ("ListEngines did not fail" )
96
+ }
97
+
98
+ var apiErr * APIError
99
+ if ! errors .As (err , & apiErr ) {
100
+ t .Fatalf ("Error is not an APIError: %+v" , err )
101
+ }
102
+
103
+ if apiErr .StatusCode != 401 {
104
+ t .Fatalf ("Unexpected API error status code: %d" , apiErr .StatusCode )
105
+ }
106
+ if * apiErr .Code != "invalid_api_key" {
107
+ t .Fatalf ("Unexpected API error code: %s" , * apiErr .Code )
108
+ }
109
+ }
110
+
111
+ func TestRequestError (t * testing.T ) {
112
+ var err error
113
+ c := NewClient ("dummy" )
114
+ c .BaseURL = "https://httpbin.org/status/418?"
115
+ ctx := context .Background ()
116
+ _ , err = c .ListEngines (ctx )
117
+ if err == nil {
118
+ t .Fatal ("ListEngines request did not fail" )
119
+ }
120
+
121
+ var reqErr * RequestError
122
+ if ! errors .As (err , & reqErr ) {
123
+ t .Fatalf ("Error is not a RequestError: %+v" , err )
124
+ }
125
+
126
+ if reqErr .StatusCode != 418 {
127
+ t .Fatalf ("Unexpected request error status code: %d" , reqErr .StatusCode )
128
+ }
129
+ }
130
+
84
131
// numTokens Returns the number of GPT-3 encoded tokens in the given text.
85
132
// This function approximates based on the rule of thumb stated by OpenAI:
86
133
// https://beta.openai.com/tokenizer
0 commit comments