Skip to content

Commit a33a0db

Browse files
committed
refactor error handling
1 parent cb5658d commit a33a0db

File tree

2 files changed

+54
-27
lines changed

2 files changed

+54
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
//
22
// AsyncImageErrors.swift
3-
//
3+
//
44
//
55
// Created by Igor on 18.02.2023.
66
//
77

88
import Foundation
9+
import async_http_client
910

1011
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
11-
enum AsyncImageErrors: Error, Equatable {
12-
13-
/// Error indicating that an image could not be created from a `uiImage`
14-
case imageInit
15-
16-
/// Error indicating that the client was not found, likely due to an invalid URL
17-
case clientIsNotDefined
18-
19-
/// Error indicating that the response returned no images
20-
case returnedNoImages
21-
22-
/// Status is not valid
23-
case httpStatus(String)
12+
/// Enum representing different errors that can occur when loading images asynchronously
13+
enum AsyncImageErrors: Error {
14+
case imageInit // Error initializing an image from data
15+
case clientIsNotDefined // HTTP client is not defined
16+
case returnedNoImages // No images were returned in the response
17+
case httpStatus(String) // HTTP status error with a message
18+
case responseError(Error) // Generic response error
2419
}
2520

2621
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
@@ -33,8 +28,50 @@ extension AsyncImageErrors: LocalizedError {
3328
return NSLocalizedString("Client not found. The URL might be invalid.", comment: "")
3429
case .returnedNoImages:
3530
return NSLocalizedString("The response did not contain any images.", comment: "")
36-
case .httpStatus(let data):
37-
return NSLocalizedString("HTTP status error: \(data).", comment: "")
31+
case .httpStatus(let status):
32+
return NSLocalizedString("HTTP status error: \(status).", comment: "")
33+
case .responseError(let error):
34+
return error.localizedDescription
35+
}
36+
}
37+
}
38+
39+
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
40+
extension AsyncImageErrors {
41+
/// Handles errors that occur during the request
42+
/// - Parameter error: The error that occurred
43+
/// - Returns: An instance of `AsyncImageErrors`
44+
static func handleRequest(_ error: Error) -> AsyncImageErrors {
45+
if let httpError = error as? Http.Errors,
46+
case let .status(_, _, data) = httpError,
47+
let responseData = data {
48+
return decodeErrorResponse(from: responseData)
3849
}
50+
return .responseError(error)
3951
}
4052
}
53+
54+
/// Decodes the error response data
55+
/// - Parameter responseData: The response data to decode
56+
/// - Returns: An instance of `AsyncImageErrors` with a decoded message
57+
fileprivate func decodeErrorResponse(from responseData: Data) -> AsyncImageErrors {
58+
if let apiResponse = try? JSONDecoder().decode(ErrorResponseWrapper.self, from: responseData) {
59+
return .httpStatus(apiResponse.error.message)
60+
}
61+
62+
let dataString = String(data: responseData, encoding: .utf8) ?? "Unable to decode data"
63+
return .httpStatus(dataString)
64+
}
65+
66+
/// Defines the structure for the inner "error" object in the API response
67+
fileprivate struct ErrorResponse: Decodable {
68+
let code: String?
69+
let message: String
70+
let param: String?
71+
let type: String
72+
}
73+
74+
/// Defines the structure for the overall response wrapper containing the error object
75+
fileprivate struct ErrorResponseWrapper: Decodable {
76+
let error: ErrorResponse
77+
}

Sources/openai-async-image-swiftui/viewModel/OpenAIDefaultLoader.swift

+1-11
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public final class OpenAIDefaultLoader: IOpenAILoader {
5858
return try imageBase64(from: result.value)
5959

6060
} catch {
61-
try handleRequestError(error)
61+
throw AsyncImageErrors.handleRequest(error)
6262
}
6363
}
6464

@@ -74,16 +74,6 @@ public final class OpenAIDefaultLoader: IOpenAILoader {
7474
return (path, body, headers)
7575
}
7676

77-
/// Handles errors that occur during the request
78-
/// - Parameter error: The error that occurred
79-
private func handleRequestError(_ error: Error) throws -> Never {
80-
if case let Http.Errors.status(_, _, data) = error, let responseData = data {
81-
let data = String(data: responseData, encoding: .utf8) ?? "Unable to decode data"
82-
throw AsyncImageErrors.httpStatus(data)
83-
}
84-
85-
throw error
86-
}
8777

8878
/// Decodes base64 encoded string to Data
8979
/// - Parameter output: The output received from the endpoint

0 commit comments

Comments
 (0)