diff --git a/Sources/OpenAI/OpenAI.swift b/Sources/OpenAI/OpenAI.swift index 3dcad3c9..cfb1e9d1 100644 --- a/Sources/OpenAI/OpenAI.swift +++ b/Sources/OpenAI/OpenAI.swift @@ -127,29 +127,16 @@ extension OpenAI { timeoutInterval: configuration.timeoutInterval) let task = session.dataTask(with: request) { data, _, error in if let error = error { - completion(.failure(error)) - return + return completion(.failure(error)) } guard let data = data else { - completion(.failure(OpenAIError.emptyData)) - return + return completion(.failure(OpenAIError.emptyData)) } - - var apiError: Error? = nil + let decoder = JSONDecoder() do { - let decoded = try JSONDecoder().decode(ResultType.self, from: data) - completion(.success(decoded)) + completion(.success(try decoder.decode(ResultType.self, from: data))) } catch { - apiError = error - } - - if let apiError = apiError { - do { - let decoded = try JSONDecoder().decode(APIErrorResponse.self, from: data) - completion(.failure(decoded)) - } catch { - completion(.failure(apiError)) - } + completion(.failure((try? decoder.decode(APIErrorResponse.self, from: data)) ?? error)) } } task.resume() @@ -189,25 +176,13 @@ extension OpenAI { let task = session.dataTask(with: request) { data, _, error in if let error = error { - completion(.failure(error)) - return + return completion(.failure(error)) } guard let data = data else { - completion(.failure(OpenAIError.emptyData)) - return + return completion(.failure(OpenAIError.emptyData)) } completion(.success(AudioSpeechResult(audioData: data))) - let apiError: Error? = nil - - if let apiError = apiError { - do { - let decoded = try JSONDecoder().decode(APIErrorResponse.self, from: data) - completion(.failure(decoded)) - } catch { - completion(.failure(apiError)) - } - } } task.resume() } catch { diff --git a/Sources/OpenAI/Private/StreamingSession.swift b/Sources/OpenAI/Private/StreamingSession.swift index a69e46cf..b986dc7e 100644 --- a/Sources/OpenAI/Private/StreamingSession.swift +++ b/Sources/OpenAI/Private/StreamingSession.swift @@ -75,26 +75,17 @@ extension StreamingSession { onProcessingError?(self, StreamingError.unknownContent) return } - - var apiError: Error? = nil + let decoder = JSONDecoder() do { - let decoder = JSONDecoder() let object = try decoder.decode(ResultType.self, from: jsonData) onReceiveContent?(self, object) } catch { - apiError = error - } - - if let apiError = apiError { - do { - let decoded = try JSONDecoder().decode(APIErrorResponse.self, from: jsonData) + if let decoded = try? decoder.decode(APIErrorResponse.self, from: jsonData) { onProcessingError?(self, decoded) - } catch { - if index == jsonObjects.count - 1 { - previousChunkBuffer = "data: \(jsonContent)" // Chunk ends in a partial JSON - } else { - onProcessingError?(self, apiError) - } + } else if index == jsonObjects.count - 1 { + previousChunkBuffer = "data: \(jsonContent)" // Chunk ends in a partial JSON + } else { + onProcessingError?(self, error) } } }