diff --git a/Sources/OpenAI/Private/StreamingSession.swift b/Sources/OpenAI/Private/StreamingSession.swift index f85c0347..77b7ba0e 100644 --- a/Sources/OpenAI/Private/StreamingSession.swift +++ b/Sources/OpenAI/Private/StreamingSession.swift @@ -62,12 +62,23 @@ final class StreamingSession: NSObject, Identifiable, URLSe onProcessingError?(self, StreamingError.unknownContent) return } + + var apiError: Error? = nil do { let decoder = JSONDecoder() let object = try decoder.decode(ResultType.self, from: jsonData) onReceiveContent?(self, object) } catch { - onProcessingError?(self, error) + apiError = error + } + + if let apiError = apiError { + do { + let decoded = try JSONDecoder().decode(APIErrorResponse.self, from: data) + onProcessingError?(self, decoded) + } catch { + onProcessingError?(self, apiError) + } } } }