Fix OpenRouter error messages being swallowed as generic HTTP status

ErrorDetail.code was typed String? but OpenRouter returns it as a JSON
number, so decoding the whole error body silently failed and every
non-200 response fell back to "Unknown error: HTTP <code>" instead of
showing OpenRouter's actual message (e.g. why image generation was
rejected). Dropped the unused code field, and made the streaming path
attempt to decode the error body too instead of skipping it outright.
This commit is contained in:
2026-08-11 10:40:07 +02:00
parent 30efc58d16
commit 087ada6ae4
2 changed files with 11 additions and 4 deletions
+1 -2
View File
@@ -485,10 +485,9 @@ struct OpenRouterImageGenerationResponse: Codable {
struct OpenRouterErrorResponse: Codable {
let error: ErrorDetail
struct ErrorDetail: Codable {
let message: String
let type: String?
let code: String?
}
}
+10 -2
View File
@@ -347,8 +347,16 @@ class OpenRouterProvider: AIProvider {
}
guard httpResponse.statusCode == 200 else {
Log.api.error("OpenRouter stream HTTP \(httpResponse.statusCode)")
continuation.finish(throwing: ProviderError.unknown("HTTP \(httpResponse.statusCode)"))
var errorBody = ""
for try await line in bytes.lines { errorBody += line }
if let errorData = errorBody.data(using: .utf8),
let errorResponse = try? JSONDecoder().decode(OpenRouterErrorResponse.self, from: errorData) {
Log.api.error("OpenRouter stream HTTP \(httpResponse.statusCode): \(errorResponse.error.message)")
continuation.finish(throwing: ProviderError.unknown(errorResponse.error.message))
} else {
Log.api.error("OpenRouter stream HTTP \(httpResponse.statusCode)")
continuation.finish(throwing: ProviderError.unknown("HTTP \(httpResponse.statusCode)"))
}
return
}