first commit
This commit is contained in:
54
core/providers/openai/errors.go
Normal file
54
core/providers/openai/errors.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
providerUtils "github.com/maximhq/bifrost/core/providers/utils"
|
||||
"github.com/maximhq/bifrost/core/schemas"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// ErrorConverter is a function that converts provider-specific error responses to BifrostError.
|
||||
type ErrorConverter func(resp *fasthttp.Response) *schemas.BifrostError
|
||||
|
||||
// ParseOpenAIError parses OpenAI error responses.
|
||||
func ParseOpenAIError(resp *fasthttp.Response) *schemas.BifrostError {
|
||||
var errorResp schemas.BifrostError
|
||||
|
||||
bifrostErr := providerUtils.HandleProviderAPIError(resp, &errorResp)
|
||||
|
||||
if errorResp.EventID != nil {
|
||||
bifrostErr.EventID = errorResp.EventID
|
||||
}
|
||||
|
||||
if errorResp.Error != nil {
|
||||
if bifrostErr.Error == nil {
|
||||
bifrostErr.Error = &schemas.ErrorField{}
|
||||
}
|
||||
bifrostErr.Error.Type = errorResp.Error.Type
|
||||
bifrostErr.Error.Code = errorResp.Error.Code
|
||||
if errorResp.Error.Message != "" {
|
||||
bifrostErr.Error.Message = errorResp.Error.Message
|
||||
}
|
||||
bifrostErr.Error.Param = errorResp.Error.Param
|
||||
if errorResp.Error.EventID != nil {
|
||||
bifrostErr.Error.EventID = errorResp.Error.EventID
|
||||
}
|
||||
}
|
||||
|
||||
if bifrostErr.Error == nil {
|
||||
bifrostErr.Error = &schemas.ErrorField{}
|
||||
}
|
||||
if strings.TrimSpace(bifrostErr.Error.Message) == "" {
|
||||
if bifrostErr.StatusCode != nil {
|
||||
bifrostErr.Error.Message = fmt.Sprintf("provider API error (status %d)", *bifrostErr.StatusCode)
|
||||
} else {
|
||||
bifrostErr.Error.Message = "provider API error"
|
||||
}
|
||||
}
|
||||
|
||||
// Set ExtraFields unconditionally so provider/model/request metadata is always attached
|
||||
|
||||
return bifrostErr
|
||||
}
|
||||
Reference in New Issue
Block a user