From e96abc9a90eb76af9c7e021753313c227f45c471 Mon Sep 17 00:00:00 2001 From: Daniel Nguyen Date: Thu, 6 Jul 2023 13:52:34 +0700 Subject: [PATCH] Fix issue where chatsStream() throws incorrect error --- Sources/OpenAI/Private/StreamingSession.swift | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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) + } } } }