@@ -18,7 +18,7 @@ enum NetworkError: LocalizedError {
1818 case transportError( Error )
1919
2020 // Received an bad response, e.g. non HTTP result.
21- case badResponse( String )
21+ case badResponse( String = " Bad response. " )
2222
2323 var rawValue : String {
2424 switch self {
@@ -71,24 +71,26 @@ final class NetworkManager: NetworkManagerProtocol {
7171 . mapError { NetworkError . transportError ( $0) }
7272 // Handle all other errors.
7373 . tryMap { element in
74- let httpResponse = element. response as? HTTPURLResponse
75- let statusCode = httpResponse? . statusCode ?? . zero
74+ guard let httpResponse = element
75+ . response as? HTTPURLResponse else {
76+ throw NetworkError . badResponse ( )
77+ }
7678
7779 #if DEBUG
78- if let httpResponse = httpResponse {
79- let message = """
80- **** CachedAsyncImage response.
81- From : \( httpResponse. url ? . absoluteString ?? " " )
82- Status code : \( statusCode )
83- """
84-
85- print ( message)
86- }
80+ if CachedAsyncImageConfiguration . shared . loggerLevel == . max {
81+ let message = """
82+ **** CachedAsyncImage response.
83+ Status code : \( httpResponse. statusCode ) ,
84+ from : \( httpResponse . url ? . absoluteString ?? " " )
85+ """
86+
87+ print ( message. replacingOccurrences ( of : " \n " , with : " " ) )
88+ }
8789 #endif
8890
89- guard statusCode == 200 else {
91+ guard ( 200 ... 299 ) . contains ( httpResponse . statusCode ) else {
9092 throw NetworkError . badResponse (
91- " Bad response. Status code \( statusCode) "
93+ " Bad response. Status code: \( httpResponse . statusCode) "
9294 )
9395 }
9496
0 commit comments