Skip to content

Commit 378228f

Browse files
committed
update
1 parent cd60e15 commit 378228f

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

Sources/openai-async-image-swiftui/OpenAIAsyncImage.swift

+9-10
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,11 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
133133
_ prompt: String,
134134
with size: ImageSize,
135135
model: DalleModel
136-
) async -> Image? {
137-
do {
136+
) async throws -> Image? {
138137
if let loader = loader {
139138
return try await loader.load(prompt, with: size, model: model)
140139
}
141140
return try await loadImageDefault(prompt, with: size, model: model)
142-
} catch {
143-
if !Task.isCancelled {
144-
self.error = error
145-
}
146-
return nil
147-
}
148141
}
149142

150143
/// Sets the image on the main thread
@@ -171,8 +164,14 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
171164
/// - Returns: A task that fetches the OpenAI image
172165
private func getTask() -> Task<Void, Never>{
173166
Task{
174-
if let image = await loadImage(prompt, with: size, model: model){
175-
await setImage(image)
167+
do{
168+
if let image = try await loadImage(prompt, with: size, model: model){
169+
setImage(image)
170+
}
171+
}catch is CancellationError{
172+
self.error = AsyncImageErrors.cancellationError
173+
}catch{
174+
self.error = error
176175
}
177176
}
178177
}

Sources/openai-async-image-swiftui/enum/AsyncImageErrors.swift

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enum AsyncImageErrors: Error {
1616
case returnedNoImages // No images were returned in the response
1717
case httpStatus(String) // HTTP status error with a message
1818
case responseError(Error) // Generic response error
19+
case cancellationError
1920
}
2021

2122
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
@@ -32,6 +33,8 @@ extension AsyncImageErrors: LocalizedError {
3233
return NSLocalizedString(description, comment: "")
3334
case .responseError(let error):
3435
return error.localizedDescription
36+
case .cancellationError:
37+
return NSLocalizedString("Cancellation error.", comment: "")
3538
}
3639
}
3740
}

0 commit comments

Comments
 (0)