@@ -10,6 +10,8 @@ import (
10
10
"github.com/google/generative-ai-go/genai"
11
11
openai "github.com/sashabaranov/go-openai"
12
12
"google.golang.org/api/option"
13
+ "google.golang.org/api/googleapi"
14
+ "github.com/pkg/errors"
13
15
14
16
"github.com/zhu327/gemini-openai-proxy/pkg/adapter"
15
17
)
@@ -78,10 +80,7 @@ func ChatProxyHandler(c *gin.Context) {
78
80
// Use fmt.Sscanf to extract the Bearer token
79
81
_ , err := fmt .Sscanf (authorizationHeader , "Bearer %s" , & openaiAPIKey )
80
82
if err != nil {
81
- c .JSON (http .StatusBadRequest , openai.APIError {
82
- Code : http .StatusBadRequest ,
83
- Message : err .Error (),
84
- })
83
+ handleGenerateContentError (c , err )
85
84
return
86
85
}
87
86
@@ -122,11 +121,7 @@ func ChatProxyHandler(c *gin.Context) {
122
121
if ! req .Stream {
123
122
resp , err := gemini .GenerateContent (ctx , req , messages )
124
123
if err != nil {
125
- log .Printf ("genai generate content error %v\n " , err )
126
- c .JSON (http .StatusBadRequest , openai.APIError {
127
- Code : http .StatusBadRequest ,
128
- Message : err .Error (),
129
- })
124
+ handleGenerateContentError (c , err )
130
125
return
131
126
}
132
127
@@ -136,11 +131,7 @@ func ChatProxyHandler(c *gin.Context) {
136
131
137
132
dataChan , err := gemini .GenerateStreamContent (ctx , req , messages )
138
133
if err != nil {
139
- log .Printf ("genai generate content error %v\n " , err )
140
- c .JSON (http .StatusBadRequest , openai.APIError {
141
- Code : http .StatusBadRequest ,
142
- Message : err .Error (),
143
- })
134
+ handleGenerateContentError (c , err )
144
135
return
145
136
}
146
137
@@ -155,6 +146,54 @@ func ChatProxyHandler(c *gin.Context) {
155
146
})
156
147
}
157
148
149
+ func handleGenerateContentError (c * gin.Context , err error ) {
150
+ log .Printf ("genai generate content error %v\n " , err )
151
+
152
+ // Try OpenAI API error first
153
+ var openaiErr * openai.APIError
154
+ if errors .As (err , & openaiErr ) {
155
+
156
+ // Convert the code to an HTTP status code
157
+ statusCode := http .StatusInternalServerError
158
+ if code , ok := openaiErr .Code .(int ); ok {
159
+ statusCode = code
160
+ }
161
+
162
+ c .AbortWithStatusJSON (statusCode , openaiErr )
163
+ return
164
+ }
165
+
166
+ // Try Google API error
167
+ var googleErr * googleapi.Error
168
+ if errors .As (err , & googleErr ) {
169
+ log .Printf ("Handling Google API error with code: %d\n " , googleErr .Code )
170
+ statusCode := googleErr .Code
171
+ if statusCode == http .StatusTooManyRequests {
172
+ c .AbortWithStatusJSON (http .StatusTooManyRequests , openai.APIError {
173
+ Code : http .StatusTooManyRequests ,
174
+ Message : "Rate limit exceeded" ,
175
+ Type : "rate_limit_error" ,
176
+ })
177
+ return
178
+ }
179
+
180
+ c .AbortWithStatusJSON (statusCode , openai.APIError {
181
+ Code : statusCode ,
182
+ Message : googleErr .Message ,
183
+ Type : "server_error" ,
184
+ })
185
+ return
186
+ }
187
+
188
+ // For all other errors
189
+ log .Printf ("Handling unknown error: %v\n " , err )
190
+ c .AbortWithStatusJSON (http .StatusInternalServerError , openai.APIError {
191
+ Code : http .StatusInternalServerError ,
192
+ Message : err .Error (),
193
+ Type : "server_error" ,
194
+ })
195
+ }
196
+
158
197
func setEventStreamHeaders (c * gin.Context ) {
159
198
c .Writer .Header ().Set ("Content-Type" , "text/event-stream" )
160
199
c .Writer .Header ().Set ("Cache-Control" , "no-cache" )
@@ -171,10 +210,7 @@ func EmbeddingProxyHandler(c *gin.Context) {
171
210
// Use fmt.Sscanf to extract the Bearer token
172
211
_ , err := fmt .Sscanf (authorizationHeader , "Bearer %s" , & openaiAPIKey )
173
212
if err != nil {
174
- c .JSON (http .StatusBadRequest , openai.APIError {
175
- Code : http .StatusBadRequest ,
176
- Message : err .Error (),
177
- })
213
+ handleGenerateContentError (c , err )
178
214
return
179
215
}
180
216
@@ -214,11 +250,7 @@ func EmbeddingProxyHandler(c *gin.Context) {
214
250
215
251
resp , err := gemini .GenerateEmbedding (ctx , messages )
216
252
if err != nil {
217
- log .Printf ("genai generate content error %v\n " , err )
218
- c .JSON (http .StatusBadRequest , openai.APIError {
219
- Code : http .StatusBadRequest ,
220
- Message : err .Error (),
221
- })
253
+ handleGenerateContentError (c , err )
222
254
return
223
255
}
224
256
0 commit comments