Skip to content

Commit

Permalink
implement errorDescription for OpenAIError
Browse files Browse the repository at this point in the history
  • Loading branch information
leetcode-mafia committed Apr 19, 2023
1 parent a0f7088 commit d3b668b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions OpenAISwift/OpenAISwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ public enum OpenAIError: Error {
case decodingError(error: Error)
}

extension OpenAIError: LocalizedError {
public var errorDescription: String? {
switch self {
case .genericError(let error), .decodingError(let error):
return error.localizedDescription
}
}
}

public class OpenAISwift {
fileprivate(set) var token: String?
fileprivate let config: Config
Expand Down Expand Up @@ -132,6 +141,9 @@ extension OpenAISwift {
let res = try JSONDecoder().decode(OpenAI<MessageResult>.self, from: success)
completionHandler(.success(res))
} catch {
if let resp = String(data: success, encoding: .utf8) {
print("Failed to decode response:\n", resp)
}
completionHandler(.failure(.decodingError(error: error)))
}
case .failure(let failure):
Expand Down

0 comments on commit d3b668b

Please sign in to comment.