1
1
//
2
2
// AsyncImageErrors.swift
3
- //
3
+ //
4
4
//
5
5
// Created by Igor on 18.02.2023.
6
6
//
7
7
8
8
import Foundation
9
+ import async_http_client
9
10
10
11
@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
24
19
}
25
20
26
21
@available ( iOS 15 . 0 , macOS 12 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
@@ -33,8 +28,50 @@ extension AsyncImageErrors: LocalizedError {
33
28
return NSLocalizedString ( " Client not found. The URL might be invalid. " , comment: " " )
34
29
case . returnedNoImages:
35
30
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)
38
49
}
50
+ return . responseError( error)
39
51
}
40
52
}
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
+ }
0 commit comments