Skip to content

Open source CapCut alternative.

TypeScript

fix(app): surface the backend's real error in generation failure reasons

David Le109ab08eca8b
diff --git a/apps/Donkey/Sources/DonkeyAI/DonkeyBackendInferenceClient.swift b/apps/Donkey/Sources/DonkeyAI/DonkeyBackendInferenceClient.swift
index 0657376b..85df55d0 100644
--- a/apps/Donkey/Sources/DonkeyAI/DonkeyBackendInferenceClient.swift
+++ b/apps/Donkey/Sources/DonkeyAI/DonkeyBackendInferenceClient.swift
@@ -19,6 +19,43 @@ public enum DonkeyBackendInferenceClientError: Error, Equatable, Sendable {
case invalidBase64(String)
}
+extension DonkeyBackendInferenceClientError {
+ /// The backend's own message from an `httpStatus` JSON error body, e.g.
+ /// {"error":"provider_error","message":"Veo video generation failed.","details":{"status":404,"message":"…"}}.
+ /// Provider errors nest the upstream provider's raw JSON in `details.message`; when that inner
+ /// text decodes as {"error":{"message":…}} the inner sentence is appended, so the actionable
+ /// cause ("model not found", "quota exceeded") reaches the tool's failure reason.
+ public var backendMessage: String? {
+ guard case .httpStatus(_, let body) = self,
+ let data = body.data(using: .utf8),
+ let object = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
+ return nil
+ }
+ var parts: [String] = []
+ if let message = object["message"] as? String, !message.isEmpty {
+ parts.append(message)
+ } else if let code = object["error"] as? String, !code.isEmpty {
+ parts.append(code)
+ }
+ if let details = object["details"] as? [String: Any],
+ let detail = details["message"] as? String, !detail.isEmpty {
+ parts.append(Self.upstreamErrorMessage(fromDetail: detail) ?? detail)
+ }
+ guard !parts.isEmpty else { return nil }
+ return String(parts.joined(separator: " ").prefix(600))
+ }
+
+ private static func upstreamErrorMessage(fromDetail detail: String) -> String? {
+ guard let data = detail.data(using: .utf8),
+ let object = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
+ let error = object["error"] as? [String: Any],
+ let message = error["message"] as? String, !message.isEmpty else {
+ return nil
+ }
+ return message
+ }
+}
+
/// Shared, app-agnostic copy for the "out of credits" state. Centralized so every consumer (the step
/// planner's fail-safe, the image generator, any future caller) surfaces one consistent message and
/// the same billing destination rather than leaking a raw `httpStatus(402, …)` dump to the user.
diff --git a/apps/Donkey/Sources/DonkeyAI/HostedImageGenerator.swift b/apps/Donkey/Sources/DonkeyAI/HostedImageGenerator.swift
index 6257f367..075b2432 100644
--- a/apps/Donkey/Sources/DonkeyAI/HostedImageGenerator.swift
+++ b/apps/Donkey/Sources/DonkeyAI/HostedImageGenerator.swift
@@ -111,7 +111,7 @@ public struct HostedImageGenerator: Sendable {
} catch {
return HarnessImageGenerationResult(
savedPaths: [],
- failureReason: "Image generation timed out or could not reach the model."
+ failureReason: Self.generationFailureReason("Image generation", for: error)
)
}
@@ -159,6 +159,21 @@ public struct HostedImageGenerator: Sendable {
return "Image generation \(record.status.rawValue)."
}
+ /// The catch-all failure text for a generation request that threw. The backend names the real
+ /// failure in its error body (provider reject, bad request), so that message is surfaced for the
+ /// planner to act on; only a genuine deadline or transport failure falls back to generic wording.
+ /// Shared with the video tool so both asset generators report errors the same way.
+ static func generationFailureReason(_ operation: String, for error: Error) -> String {
+ if error is AIDeadline.Exceeded {
+ return "\(operation) timed out."
+ }
+ if let clientError = error as? DonkeyBackendInferenceClientError,
+ let message = clientError.backendMessage {
+ return "\(operation) failed: \(message)"
+ }
+ return "\(operation) could not reach the model."
+ }
+
private static func errorText(_ value: RemoteInferenceJSONValue?) -> String? {
switch value {
case .string(let text):
diff --git a/apps/Donkey/Sources/DonkeyAI/HostedVideoGenerator.swift b/apps/Donkey/Sources/DonkeyAI/HostedVideoGenerator.swift
index b7666f35..5bc05495 100644
--- a/apps/Donkey/Sources/DonkeyAI/HostedVideoGenerator.swift
+++ b/apps/Donkey/Sources/DonkeyAI/HostedVideoGenerator.swift
@@ -116,7 +116,7 @@ public struct HostedVideoGenerator: Sendable {
} catch {
return HarnessVideoGenerationResult(
savedPaths: [],
- failureReason: "Video generation timed out or could not reach the model."
+ failureReason: HostedImageGenerator.generationFailureReason("Video generation", for: error)
)
}
}