mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-10-02 21:59:34 +08:00
- Introduce `modelExecutionStatusError` to retain explicit HTTP status codes on model execution errors - Propagate HTTP status codes from callback errors across Unix and Windows host callback dispatchers - Update `marshalRPCError` to encode HTTP status codes into error envelopes using `pluginabi.NewErrorEnvelope` Closes: #5970
134 lines
4.2 KiB
Go
134 lines
4.2 KiB
Go
package pluginhost
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginabi"
|
|
)
|
|
|
|
type staticEnvelopePluginClient struct {
|
|
raw []byte
|
|
}
|
|
|
|
func (c staticEnvelopePluginClient) Call(context.Context, string, []byte) ([]byte, error) {
|
|
return c.raw, nil
|
|
}
|
|
|
|
func (c staticEnvelopePluginClient) Shutdown() {}
|
|
|
|
func TestDecodeEnvelopeResultPreservesPluginHTTPStatus(t *testing.T) {
|
|
_, errDecode := decodeEnvelopeResult[rpcEmptyResponse](pluginabi.Envelope{
|
|
OK: false,
|
|
Error: &pluginabi.Error{
|
|
Code: "plugin_error",
|
|
Message: "license required",
|
|
HTTPStatus: http.StatusForbidden,
|
|
},
|
|
})
|
|
if errDecode == nil {
|
|
t.Fatal("decodeEnvelopeResult returned nil error")
|
|
}
|
|
if got := errDecode.Error(); got != "license required" {
|
|
t.Fatalf("error = %q, want license required", got)
|
|
}
|
|
statusProvider, ok := errDecode.(interface{ StatusCode() int })
|
|
if !ok {
|
|
t.Fatalf("error %T does not expose StatusCode", errDecode)
|
|
}
|
|
if got := statusProvider.StatusCode(); got != http.StatusForbidden {
|
|
t.Fatalf("status = %d, want %d", got, http.StatusForbidden)
|
|
}
|
|
}
|
|
|
|
func TestCallPluginReturnsPluginErrorWithoutMethodWrapper(t *testing.T) {
|
|
raw, errMarshal := json.Marshal(pluginabi.Envelope{
|
|
OK: false,
|
|
Error: &pluginabi.Error{
|
|
Code: "plugin_error",
|
|
Message: "license required",
|
|
HTTPStatus: http.StatusForbidden,
|
|
},
|
|
})
|
|
if errMarshal != nil {
|
|
t.Fatalf("marshal envelope: %v", errMarshal)
|
|
}
|
|
_, errCall := callPlugin[rpcEmptyResponse](context.Background(), staticEnvelopePluginClient{raw: raw}, pluginabi.MethodExecutorExecuteStream, rpcEmptyResponse{})
|
|
if errCall == nil {
|
|
t.Fatal("callPlugin returned nil error")
|
|
}
|
|
if got := errCall.Error(); got != "license required" {
|
|
t.Fatalf("error = %q, want license required", got)
|
|
}
|
|
statusProvider, ok := errCall.(interface{ StatusCode() int })
|
|
if !ok {
|
|
t.Fatalf("error %T does not expose StatusCode", errCall)
|
|
}
|
|
if got := statusProvider.StatusCode(); got != http.StatusForbidden {
|
|
t.Fatalf("status = %d, want %d", got, http.StatusForbidden)
|
|
}
|
|
}
|
|
|
|
func TestIsPluginErrorEnvelopeAcceptsNonzeroReturnEnvelope(t *testing.T) {
|
|
raw := marshalRPCError("plugin_error", "upstream failed")
|
|
if !isPluginErrorEnvelope(raw) {
|
|
t.Fatalf("isPluginErrorEnvelope(%s) = false, want true", raw)
|
|
}
|
|
if isPluginErrorEnvelope([]byte(`not json`)) {
|
|
t.Fatal("isPluginErrorEnvelope accepted invalid JSON")
|
|
}
|
|
}
|
|
|
|
func TestCallPluginPreservesStatusFromNewErrorEnvelope(t *testing.T) {
|
|
raw, errMarshal := pluginabi.NewErrorEnvelope("insufficient_quota", "plan limit reached", http.StatusForbidden)
|
|
if errMarshal != nil {
|
|
t.Fatalf("NewErrorEnvelope() error = %v", errMarshal)
|
|
}
|
|
_, errCall := callPlugin[rpcEmptyResponse](context.Background(), staticEnvelopePluginClient{raw: raw}, pluginabi.MethodExecutorExecute, rpcEmptyResponse{})
|
|
if errCall == nil {
|
|
t.Fatal("callPlugin returned nil error")
|
|
}
|
|
if got := errCall.Error(); got != "plan limit reached" {
|
|
t.Fatalf("error = %q, want plan limit reached", got)
|
|
}
|
|
statusProvider, ok := errCall.(interface{ StatusCode() int })
|
|
if !ok {
|
|
t.Fatalf("error %T does not expose StatusCode", errCall)
|
|
}
|
|
if got := statusProvider.StatusCode(); got != http.StatusForbidden {
|
|
t.Fatalf("status = %d, want %d", got, http.StatusForbidden)
|
|
}
|
|
}
|
|
|
|
func TestMarshalRPCErrorPreservesHTTPStatus(t *testing.T) {
|
|
for _, status := range []int{http.StatusTooManyRequests, http.StatusServiceUnavailable} {
|
|
raw := marshalRPCError("host_call_failed", "synthetic", status)
|
|
var env pluginabi.Envelope
|
|
if errUnmarshal := json.Unmarshal(raw, &env); errUnmarshal != nil {
|
|
t.Fatalf("unmarshal envelope: %v", errUnmarshal)
|
|
}
|
|
if env.OK {
|
|
t.Fatal("expected envelope OK=false")
|
|
}
|
|
if env.Error == nil {
|
|
t.Fatal("expected non-nil Error in envelope")
|
|
}
|
|
if env.Error.HTTPStatus != status {
|
|
t.Fatalf("HTTPStatus = %d, want %d", env.Error.HTTPStatus, status)
|
|
}
|
|
_, errDecode := decodeEnvelopeResult[rpcEmptyResponse](env)
|
|
if errDecode == nil {
|
|
t.Fatal("expected decode error")
|
|
}
|
|
statusProvider, ok := errDecode.(interface{ StatusCode() int })
|
|
if !ok {
|
|
t.Fatalf("decoded error does not expose StatusCode: %T", errDecode)
|
|
}
|
|
if got := statusProvider.StatusCode(); got != status {
|
|
t.Fatalf("StatusCode = %d, want %d", got, status)
|
|
}
|
|
}
|
|
}
|