Files
new-api/relay/param_override_error_test.go
Calcium-Ion 1c63cc8c90 fix(relay): keep request-triggered errors from disabling channels (#8345)
Errors new-api raises itself because of the request no longer count
against the channel:

- A param override condition that meets a request value of another type
  (for example a string temperature in a numeric gt rule) now returns a
  400 invalid_request that is not retried, instead of
  channel:param_override_invalid, which was retried on every channel and
  could auto-disable each of them. Invalid rules that fail every request
  (non-numeric rule value, unsupported mode) stay channel errors.
- Image conversion failures use the shared convert-request-failed error,
  so they are no longer retried on other channels.
- Automatic channel disabling skips errors classified as local by the
  request policy log, so a local status or a disable keyword echoed from
  client input cannot disable a channel. Upstream responses that adaptors
  report as new-api errors, and local wrappers around an upstream error,
  keep their upstream classification. The logged health entry now uses
  the same decision.
2026-11-16 01:33:17 +08:00

49 lines
2.1 KiB
Go

package relay
import (
"net/http"
"testing"
relaycommon "github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/service"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// A param override failure is a request error when the request's own value
// broke a valid condition, and a channel error when the rule itself is
// invalid and would fail every request on the channel.
func TestParamOverrideFailureClassification(t *testing.T) {
condition := func(mode string, value any) map[string]any {
return map[string]any{"operations": []any{map[string]any{
"path": "max_tokens", "mode": "set", "value": 512,
"conditions": []any{map[string]any{"path": "temperature", "mode": mode, "value": value}},
}}}
}
for _, tc := range []struct {
name string
body string
override map[string]any
code types.ErrorCode
status int
source string
}{
{name: "numeric rule meets a string request value", body: `{"temperature":"hot"}`, override: condition("gt", 1), code: types.ErrorCodeInvalidRequest, status: http.StatusBadRequest, source: "local"},
{name: "equality rule meets a request value of another type", body: `{"temperature":"1"}`, override: condition("full", 1), code: types.ErrorCodeInvalidRequest, status: http.StatusBadRequest, source: "local"},
{name: "numeric rule with a non-numeric rule value", body: `{"temperature":2}`, override: condition("gt", "1"), code: types.ErrorCodeChannelParamOverrideInvalid, status: http.StatusInternalServerError, source: "channel"},
{name: "unsupported comparison mode", body: `{"temperature":2}`, override: condition("between", 1), code: types.ErrorCodeChannelParamOverrideInvalid, status: http.StatusInternalServerError, source: "channel"},
} {
t.Run(tc.name, func(t *testing.T) {
_, err := relaycommon.ApplyParamOverride([]byte(tc.body), tc.override, nil)
require.Error(t, err)
apiErr := newAPIErrorFromParamOverride(err)
assert.Equal(t, tc.code, apiErr.GetErrorCode())
assert.Equal(t, tc.status, apiErr.StatusCode)
assert.Equal(t, tc.source, service.RelayErrorSource(apiErr))
})
}
}