Files
bifrost/core/providers/openai/errors.go
Beyhan Oğur 880f412e2c first commit
2026-04-26 21:52:23 +03:00

55 lines
1.5 KiB
Go

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
}