Skip to content

Commit ff96ce2

Browse files
Merge pull request #25 from AppcentMobile/feature/network-error-statuscode
🎷 [UPDATE] HTTP status code added for network error.
2 parents d260435 + aa086c6 commit ff96ce2

File tree

10 files changed

+23
-20
lines changed

10 files changed

+23
-20
lines changed

Sources/ACMNetworking/Library/Callbacks/ACMGenericCallbacks.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public enum ACMGenericCallbacks {
1111
/// Void callback for generic closures
1212
public typealias VoidCallback = (() -> Void)?
1313
/// Error callback for generic closures
14-
public typealias ErrorCallback = ((Error?) -> Void)?
14+
public typealias ErrorCallback = ((ACMBaseNetworkError?) -> Void)?
1515
/// Info callback with success check and error for generic closures
1616
public typealias InfoCallback = ((Bool?, Error?) -> Void)?
1717
/// Success callback with generic response for closures
@@ -21,6 +21,5 @@ public enum ACMGenericCallbacks {
2121
/// Progress callback with generic response for closures
2222
public typealias ProgressCallback = ((ACMProgressModel) -> Void)?
2323
/// Success callback with generic response for closures
24-
public typealias StreamCallback = ((Data) -> Void)
25-
24+
public typealias StreamCallback = (Data) -> Void
2625
}

Sources/ACMNetworking/Library/Constants/Plist/ACMPListContants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import Foundation
66

7-
struct ACMPListContants {
7+
enum ACMPListContants {
88
static let fileName = "ACMConfig"
99
static let fileExtension = "plist"
1010
}

Sources/ACMNetworking/Library/Extensions/ACMNetworking+Extensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ extension ACMNetworking {
7474

7575
extension ACMNetworking: URLSessionTaskDelegate, URLSessionDelegate, URLSessionDataDelegate {
7676
/// URL Session data task for stream requests
77-
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
77+
public func urlSession(_: URLSession, dataTask _: URLSessionDataTask, didReceive data: Data) {
7878
let dataString = String(data: data, encoding: .utf8) ?? ""
7979
if !dataString.contains("[DONE]") {
8080
let response = dataString.components(separatedBy: "\n")
81-
.filter{ !$0.replacingOccurrences(of: " ", with: "").isEmpty }
81+
.filter { !$0.replacingOccurrences(of: " ", with: "").isEmpty }
8282
.map { $0.replacingOccurrences(of: "data:", with: "")
8383
.replacingOccurrences(of: " ", with: "")
8484
}

Sources/ACMNetworking/Library/Extensions/ACMNetworking+Handle+Extensions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ extension ACMNetworking {
4444
ACMNetworkConstants.responseNullMessage,
4545
])
4646
endpoint.logger?.error(message)
47-
onError?(ACMBaseNetworkError(message: ACMNetworkConstants.errorMessage, log: ACMNetworkConstants.responseNullMessage, endpoint: endpoint))
47+
onError?(ACMBaseNetworkError(message: ACMNetworkConstants.errorMessage, log: ACMNetworkConstants.responseNullMessage, endpoint: endpoint, statusCode: 200))
4848
return
4949
}
5050
}
@@ -141,7 +141,7 @@ extension ACMNetworking {
141141

142142
extension ACMNetworking {
143143
/// Handle server response
144-
func handleResult<T: Decodable>(with endpoint: ACMBaseEndpoint, data: Data, onSuccess: ACMGenericCallbacks.ResponseCallback<T>, onError: ACMGenericCallbacks.ErrorCallback) {
144+
func handleResult<T: Decodable>(with endpoint: ACMBaseEndpoint, httpResponse: HTTPURLResponse, data: Data, onSuccess: ACMGenericCallbacks.ResponseCallback<T>, onError: ACMGenericCallbacks.ErrorCallback) {
145145
do {
146146
let dataString = String(data: data, encoding: .utf8) ?? ""
147147
let info = endpoint.stringUtils?.merge(list: [
@@ -195,7 +195,7 @@ extension ACMNetworking {
195195
} catch let e {
196196
let errorMessage = String(format: ACMNetworkConstants.dataParseErrorMessage, e.localizedDescription)
197197
endpoint.logger?.warning(errorMessage)
198-
onError?(ACMBaseNetworkError(message: ACMNetworkConstants.errorMessage, log: errorMessage, endpoint: endpoint))
198+
onError?(ACMBaseNetworkError(message: ACMNetworkConstants.errorMessage, log: errorMessage, endpoint: endpoint, statusCode: httpResponse.statusCode))
199199
}
200200
}
201201
}

Sources/ACMNetworking/Library/MainCalls/ACMNetworking+Request.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import Foundation
99

1010
public extension ACMNetworking {
1111
func stream(to endpoint: ACMBaseEndpoint,
12-
currentRetryCount: Int? = 0,
12+
currentRetryCount _: Int? = 0,
1313
onPartial: @escaping ACMGenericCallbacks.StreamCallback,
1414
onProgress: ACMGenericCallbacks.ProgressCallback = nil,
15-
onError: ACMGenericCallbacks.ErrorCallback = nil) {
15+
onError _: ACMGenericCallbacks.ErrorCallback = nil)
16+
{
1617
self.onPartial = onPartial
1718

1819
session = endpoint.session(delegate: self)
@@ -40,9 +41,10 @@ public extension ACMNetworking {
4041
func request<T: Decodable>(to endpoint: ACMBaseEndpoint,
4142
currentRetryCount: Int? = 0,
4243
onSuccess: ACMGenericCallbacks.ResponseCallback<T>,
43-
onPartial: ACMGenericCallbacks.ResponseCallback<T> = nil,
44+
onPartial _: ACMGenericCallbacks.ResponseCallback<T> = nil,
4445
onProgress: ACMGenericCallbacks.ProgressCallback = nil,
45-
onError: ACMGenericCallbacks.ErrorCallback = nil) {
46+
onError: ACMGenericCallbacks.ErrorCallback = nil)
47+
{
4648
guard let urlRequest = generateURLRequest(endpoint: endpoint) else { return }
4749

4850
session = endpoint.session(delegate: self)
@@ -65,7 +67,7 @@ public extension ACMNetworking {
6567

6668
self.cancel()
6769

68-
self.handleResult(with: endpoint, data: data, onSuccess: onSuccess, onError: onError)
70+
self.handleResult(with: endpoint, httpResponse: httpResponse, data: data, onSuccess: onSuccess, onError: onError)
6971
}
7072

7173
taskProgress = requestTask?.progress.observe(\.fractionCompleted, changeHandler: { progress, _ in

Sources/ACMNetworking/Library/Manager/ACMBaseEndpoint.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ public struct ACMBaseEndpoint {
148148
}
149149

150150
init(config: ACMPlistModel? = nil, configOverride: Bool? = nil, host: String? = nil, scheme: ACMBaseScheme? = nil, path: String = "", queryItems: [URLQueryItem]? = nil, params: [String: Any?]? = nil, headers: NSMutableDictionary? = nil, method: ACMBaseMethod? = nil, authHeader: ACMAuthModel? = nil, mediaData: NSMutableData? = nil, retryCount: Int? = nil, isStream: Bool = false, downloadURL: String? = nil) {
151-
self.plistUtils = ACMPlistUtils()
151+
plistUtils = ACMPlistUtils()
152152
if let config = config {
153153
self.config = config
154154
} else {
155-
self.config = self.plistUtils?.config()
155+
self.config = plistUtils?.config()
156156
}
157157
logger = ACMBaseLogger(config: self.config)
158158
stringUtils = ACMStringUtils()

Sources/ACMNetworking/Library/Manager/ACMBaseNetworkError.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// ACMBaseNetworkError.swift
33
//
44

5-
struct ACMBaseNetworkError: Error {
5+
public struct ACMBaseNetworkError: Error {
66
/// Message holds the error description
77
var message: String?
88
/// Log holds the localization description
99
var log: String?
1010
/// Endpoint holds the current endpoint that calls
1111
var endpoint: ACMBaseEndpoint?
12+
/// Status code
13+
var statusCode: Int?
1214
}

Sources/ACMNetworking/Library/Utils/Body/ACMBodyEncoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Foundation
77
/// ACMBodyEncoder
88
///
99
/// Encoder for body payload
10-
final class ACMBodyEncoder {
10+
enum ACMBodyEncoder {
1111
/// Encode function
1212
/// For creating body payload
1313
/// - Parameters:

Sources/ACMNetworking/Library/Utils/Headers/ACMHeadersEncoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Foundation
77
/// ACMHeadersEncoder
88
///
99
/// Encoder for headers
10-
final class ACMHeadersEncoder {
10+
enum ACMHeadersEncoder {
1111
/// Encode function
1212
/// For creating headers
1313
/// - Parameters:

Sources/ACMNetworking/Library/Utils/QueryParams/ACMQueryParamEncoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Foundation
77
/// ACMQueryParamEncoder
88
///
99
/// Encoder for query parameters
10-
final class ACMQueryParamEncoder {
10+
enum ACMQueryParamEncoder {
1111
/// Static function for encode
1212
/// Creates queryitem list with given query model list
1313
/// - Parameters:

0 commit comments

Comments
 (0)