fix(plugin): 万相3.0视频 - 修复上游响应错误解析
This commit is contained in:
@@ -48,27 +48,43 @@ func providerResponseBusinessFailure(responseBody []byte) (string, string, bool)
|
||||
}
|
||||
|
||||
func providerPayloadBusinessFailure(payload map[string]any) (string, string, bool) {
|
||||
if code, message, failed := providerBusinessFailure(payload); failed {
|
||||
return code, message, true
|
||||
}
|
||||
// DashScope 业务失败在 output 内
|
||||
if output, ok := payload["output"].(map[string]any); ok {
|
||||
return providerBusinessFailure(output)
|
||||
}
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
func providerBusinessFailure(payload map[string]any) (string, string, bool) {
|
||||
if errorValue, ok := payload["error"].(map[string]any); ok {
|
||||
code, message := providerFailureDetails(map[string]any{"error": errorValue})
|
||||
if code != "" || message != "" {
|
||||
return code, message, true
|
||||
}
|
||||
}
|
||||
if !providerBusinessCodeFailed(payload["code"]) {
|
||||
return "", "", false
|
||||
}
|
||||
code, message := providerFailureDetails(payload)
|
||||
return code, message, true
|
||||
}
|
||||
|
||||
func providerBusinessCodeFailed(value any) bool {
|
||||
code := strings.ToLower(strings.TrimSpace(fmt.Sprint(value)))
|
||||
switch code {
|
||||
case "", "0", "success", "succeeded", "ok", "<nil>":
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
code := strings.ToLower(strings.TrimSpace(fmt.Sprint(payload["code"])))
|
||||
if code != "" && code != "0" &&
|
||||
code != "success" && code != "succeeded" &&
|
||||
code != "ok" && code != "<nil>" {
|
||||
code, message := providerFailureDetails(payload)
|
||||
return code, message, true
|
||||
}
|
||||
|
||||
status := strings.ToLower(strings.TrimSpace(fmt.Sprint(payload["task_status"])))
|
||||
switch status {
|
||||
case "failed", "failure", "error", "expired":
|
||||
code, message := providerFailureDetails(payload)
|
||||
if code == "" {
|
||||
code = "task_failed"
|
||||
}
|
||||
return code, message, true
|
||||
}
|
||||
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
func normalizedProviderErrorCode(value any) string {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package app
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestProviderFailureDetailsReadsTopLevelModerationError(t *testing.T) {
|
||||
code, message := providerFailureDetails(map[string]any{
|
||||
@@ -59,3 +62,30 @@ func TestProviderPayloadBusinessFailureAcceptsStringSuccessCode(t *testing.T) {
|
||||
t.Fatalf("success payload was marked failed: (%q, %q)", code, message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProviderPayloadBusinessFailureReadsNestedOutputFailure(t *testing.T) {
|
||||
code, message, failed := providerPayloadBusinessFailure(map[string]any{
|
||||
"output": map[string]any{
|
||||
"code": "InvalidParameter",
|
||||
"message": "Input should be '1080P', '720P' or '480P': parameters.resolution",
|
||||
"task_id": "e0d8c86d-4939-4f2e-93f7-e2cd10a07d58",
|
||||
"task_status": "FAILED",
|
||||
},
|
||||
"request_id": "c3c98e67-830d-9f1d-8a62-fff019ffa522",
|
||||
})
|
||||
if !failed || code != "InvalidParameter" {
|
||||
t.Fatalf("nested output failure = (%q, %q, %v)", code, message, failed)
|
||||
}
|
||||
if !strings.Contains(message, "parameters.resolution") {
|
||||
t.Fatalf("unexpected message: %q", message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProviderPayloadBusinessFailureIgnoresPendingOutput(t *testing.T) {
|
||||
if code, message, failed := providerPayloadBusinessFailure(map[string]any{
|
||||
"output": map[string]any{"task_id": "task-1", "task_status": "PENDING"},
|
||||
"request_id": "req-1",
|
||||
}); failed {
|
||||
t.Fatalf("pending output marked failed: (%q, %q)", code, message)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user