Files
new-api-custom/controller/relay_client_error_test.go
T

64 lines
2.4 KiB
Go

package controller
import (
"errors"
"fmt"
"net/http"
"strings"
"testing"
"github.com/QuantumNous/new-api/relaykit/types"
)
func TestClientFacingOpenAIErrorHidesUpstreamDetails(t *testing.T) {
err := types.NewOpenAIError(errors.New("upstream.example.invalid provider secret failure"), types.ErrorCodeBadResponseStatusCode, http.StatusBadGateway)
got := clientFacingOpenAIError(err, true, "local-request-id")
if strings.Contains(got.Message, "upstream.example") || strings.Contains(got.Message, "provider secret") {
t.Fatalf("upstream details leaked: %q", got.Message)
}
if !strings.Contains(got.Message, "local-request-id") {
t.Fatalf("local request id missing: %q", got.Message)
}
if got.Code != "model_service_error" {
t.Fatalf("unexpected public code: %v", got.Code)
}
}
func TestClientFacingClaudeErrorHidesUpstreamDetails(t *testing.T) {
err := types.NewOpenAIError(errors.New("upstream.example.invalid provider secret failure"), types.ErrorCodeBadResponseStatusCode, http.StatusBadGateway)
got := clientFacingClaudeError(err, true, "local-request-id")
if strings.Contains(got.Message, "upstream.example") || strings.Contains(got.Message, "provider secret") {
t.Fatalf("upstream details leaked: %q", got.Message)
}
if !strings.Contains(got.Message, "local-request-id") {
t.Fatalf("local request id missing: %q", got.Message)
}
}
func TestClientFacingErrorKeepsLocalValidationMessage(t *testing.T) {
err := types.NewErrorWithStatusCode(errors.New("model is required"), types.ErrorCodeInvalidRequest, http.StatusBadRequest)
got := clientFacingOpenAIError(err, false, "local-request-id")
if !strings.Contains(got.Message, "model is required") {
t.Fatalf("local validation message was hidden: %q", got.Message)
}
}
func TestClientFacingResponseGuardErrorNamesDeclaredModel(t *testing.T) {
err := types.NewErrorWithStatusCode(
errors.New("Requested model gpt-6-astra, but the upstream response declared gpt-5.6-luna."),
"response_model_rejected",
http.StatusConflict,
)
got := clientFacingOpenAIError(err, true, "local-request-id")
if fmt.Sprint(got.Code) != "response_model_rejected" {
t.Fatalf("unexpected public code: %v", got.Code)
}
for _, expected := range []string{"gpt-6-astra", "gpt-5.6-luna", "local-request-id"} {
if !strings.Contains(got.Message, expected) {
t.Fatalf("public mismatch message %q does not contain %q", got.Message, expected)
}
}
}