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:
@@ -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?
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user