11package gemini
22
33import (
4- "fmt"
54 "strconv"
65
7- "github.com/bytedance/sonic"
86 providerUtils "github.com/maximhq/bifrost/core/providers/utils"
97 "github.com/maximhq/bifrost/core/schemas"
108 "github.com/valyala/fasthttp"
119)
1210
13- // ToGeminiError derives a GeminiChatRequestError from a BifrostError
14- func ToGeminiError (bifrostErr * schemas.BifrostError ) * GeminiChatRequestError {
11+ // ToGeminiError derives a GeminiGenerationError from a BifrostError
12+ func ToGeminiError (bifrostErr * schemas.BifrostError ) * GeminiGenerationError {
1513 if bifrostErr == nil {
1614 return nil
1715 }
@@ -27,64 +25,43 @@ func ToGeminiError(bifrostErr *schemas.BifrostError) *GeminiChatRequestError {
2725 if bifrostErr .StatusCode != nil {
2826 code = * bifrostErr .StatusCode
2927 }
30- return & GeminiChatRequestError {
31- Error : GeminiChatRequestErrorStruct {
28+ return & GeminiGenerationError {
29+ Error : & GeminiGenerationErrorStruct {
3230 Code : code ,
3331 Message : message ,
3432 Status : status ,
3533 },
3634 }
3735}
3836
39- // parseStreamGeminiError parses Gemini streaming error responses
40- func parseStreamGeminiError ( providerName schemas. ModelProvider , resp * fasthttp.Response ) * schemas.BifrostError {
41- body := append ([] byte ( nil ), resp . Body () ... )
42-
43- // Try to parse as JSON first
44- var errorResp GeminiGenerationError
45- if err := sonic . Unmarshal ( body , & errorResp ); err == nil {
46- bifrostErr := & schemas. BifrostError {
47- IsBifrostError : false ,
48- StatusCode : schemas . Ptr ( int ( resp . StatusCode ())),
49- Error : & schemas. ErrorField {
50- Code : schemas . Ptr ( strconv . Itoa ( errorResp . Error . Code )),
51- Message : errorResp .Error . Message ,
52- },
37+ // parseGeminiError parses Gemini error responses
38+ func parseGeminiError ( resp * fasthttp.Response ) * schemas.BifrostError {
39+ // Try to parse as []GeminiGenerationError
40+ var errorResps [] GeminiGenerationError
41+ bifrostErr := providerUtils . HandleProviderAPIError ( resp , & errorResps )
42+ if len ( errorResps ) > 0 {
43+ var message string
44+ for _ , errorResp := range errorResps {
45+ if errorResp . Error != nil {
46+ message = message + errorResp . Error . Message + " \n "
47+ }
48+ }
49+ if bifrostErr .Error == nil {
50+ bifrostErr . Error = & schemas. ErrorField {}
5351 }
52+ bifrostErr .Error .Message = message
5453 return bifrostErr
5554 }
5655
57- // If JSON parsing fails, use the raw response body
58- var rawResponse interface {}
59- if err := sonic .Unmarshal (body , & rawResponse ); err != nil {
60- return providerUtils .NewBifrostOperationError (schemas .ErrProviderResponseUnmarshal , err , providerName )
61- }
62-
63- return providerUtils .NewBifrostOperationError (fmt .Sprintf ("Gemini streaming error (HTTP %d): %v" , resp .StatusCode (), rawResponse ), fmt .Errorf ("HTTP %d" , resp .StatusCode ()), providerName )
64- }
65-
66- // parseGeminiError parses Gemini error responses
67- func parseGeminiError (providerName schemas.ModelProvider , resp * fasthttp.Response ) * schemas.BifrostError {
68- body := append ([]byte (nil ), resp .Body ()... )
69-
70- // Try to parse as JSON first
56+ // Try to parse as GeminiGenerationError
7157 var errorResp GeminiGenerationError
72- if err := sonic .Unmarshal (body , & errorResp ); err == nil {
73- bifrostErr := & schemas.BifrostError {
74- IsBifrostError : false ,
75- StatusCode : schemas .Ptr (resp .StatusCode ()),
76- Error : & schemas.ErrorField {
77- Code : schemas .Ptr (strconv .Itoa (errorResp .Error .Code )),
78- Message : errorResp .Error .Message ,
79- },
58+ bifrostErr = providerUtils .HandleProviderAPIError (resp , & errorResp )
59+ if errorResp .Error != nil {
60+ if bifrostErr .Error == nil {
61+ bifrostErr .Error = & schemas.ErrorField {}
8062 }
81- return bifrostErr
63+ bifrostErr .Error .Code = schemas .Ptr (strconv .Itoa (errorResp .Error .Code ))
64+ bifrostErr .Error .Message = errorResp .Error .Message
8265 }
83-
84- var rawResponse map [string ]interface {}
85- if err := sonic .Unmarshal (body , & rawResponse ); err != nil {
86- return providerUtils .NewBifrostOperationError ("failed to parse error response" , err , providerName )
87- }
88-
89- return providerUtils .NewBifrostOperationError (fmt .Sprintf ("Gemini error: %v" , rawResponse ), fmt .Errorf ("HTTP %d" , resp .StatusCode ()), providerName )
66+ return bifrostErr
9067}
0 commit comments