|
|
@@ -25,6 +25,17 @@ import (
|
|
|
"github.com/aws/smithy-go/auth/bearer"
|
|
|
)
|
|
|
|
|
|
+// getAwsErrorStatusCode extracts HTTP status code from AWS SDK error
|
|
|
+func getAwsErrorStatusCode(err error) int {
|
|
|
+ // Check for HTTP response error which contains status code
|
|
|
+ var httpErr interface{ HTTPStatusCode() int }
|
|
|
+ if errors.As(err, &httpErr) {
|
|
|
+ return httpErr.HTTPStatusCode()
|
|
|
+ }
|
|
|
+ // Default to 500 if we can't determine the status code
|
|
|
+ return http.StatusInternalServerError
|
|
|
+}
|
|
|
+
|
|
|
func newAwsClient(c *gin.Context, info *relaycommon.RelayInfo) (*bedrockruntime.Client, error) {
|
|
|
var (
|
|
|
httpClient *http.Client
|
|
|
@@ -173,7 +184,8 @@ func awsHandler(c *gin.Context, info *relaycommon.RelayInfo, a *Adaptor) (*types
|
|
|
|
|
|
awsResp, err := a.AwsClient.InvokeModel(c.Request.Context(), a.AwsReq.(*bedrockruntime.InvokeModelInput))
|
|
|
if err != nil {
|
|
|
- return types.NewOpenAIError(errors.Wrap(err, "InvokeModel"), types.ErrorCodeAwsInvokeError, http.StatusInternalServerError), nil
|
|
|
+ statusCode := getAwsErrorStatusCode(err)
|
|
|
+ return types.NewOpenAIError(errors.Wrap(err, "InvokeModel"), types.ErrorCodeAwsInvokeError, statusCode), nil
|
|
|
}
|
|
|
|
|
|
claudeInfo := &claude.ClaudeResponseInfo{
|
|
|
@@ -199,7 +211,8 @@ func awsHandler(c *gin.Context, info *relaycommon.RelayInfo, a *Adaptor) (*types
|
|
|
func awsStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, a *Adaptor) (*types.NewAPIError, *dto.Usage) {
|
|
|
awsResp, err := a.AwsClient.InvokeModelWithResponseStream(c.Request.Context(), a.AwsReq.(*bedrockruntime.InvokeModelWithResponseStreamInput))
|
|
|
if err != nil {
|
|
|
- return types.NewOpenAIError(errors.Wrap(err, "InvokeModelWithResponseStream"), types.ErrorCodeAwsInvokeError, http.StatusInternalServerError), nil
|
|
|
+ statusCode := getAwsErrorStatusCode(err)
|
|
|
+ return types.NewOpenAIError(errors.Wrap(err, "InvokeModelWithResponseStream"), types.ErrorCodeAwsInvokeError, statusCode), nil
|
|
|
}
|
|
|
stream := awsResp.GetStream()
|
|
|
defer stream.Close()
|
|
|
@@ -238,7 +251,8 @@ func handleNovaRequest(c *gin.Context, info *relaycommon.RelayInfo, a *Adaptor)
|
|
|
|
|
|
awsResp, err := a.AwsClient.InvokeModel(c.Request.Context(), a.AwsReq.(*bedrockruntime.InvokeModelInput))
|
|
|
if err != nil {
|
|
|
- return types.NewError(errors.Wrap(err, "InvokeModel"), types.ErrorCodeChannelAwsClientError), nil
|
|
|
+ statusCode := getAwsErrorStatusCode(err)
|
|
|
+ return types.NewOpenAIError(errors.Wrap(err, "InvokeModel"), types.ErrorCodeAwsInvokeError, statusCode), nil
|
|
|
}
|
|
|
|
|
|
// 解析Nova响应
|