Skip to content

Commit

Permalink
Add error handler. Default impl prints, as before, custom can for eg …
Browse files Browse the repository at this point in the history
…log to sentry.
  • Loading branch information
Jasper Blues committed Sep 28, 2021
1 parent 544f712 commit b80476d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 30 deletions.
18 changes: 18 additions & 0 deletions Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@
44DFD3B01D9A34540014E9F2 /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = 148341701CF1CF0500E91F01 /* Image.swift */; };
44DFD3B11D9A3FC80014E9F2 /* pig.png in Resources */ = {isa = PBXBuildFile; fileRef = 14B0CEE31CF1D0D700049AD6 /* pig.png */; };
44DFD3B21D9A3FC90014E9F2 /* pig.png in Resources */ = {isa = PBXBuildFile; fileRef = 14B0CEE31CF1D0D700049AD6 /* pig.png */; };
BA79824C3FD10818DA976F8A /* ErrorHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorHandler.swift */; };
BA7985EE02F8B15CAE1C86DD /* ErrorHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorHandler.swift */; };
BA79866331D2E147C9A9EE21 /* ErrorHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorHandler.swift */; };
BA79878BCE133FA5429E6DAA /* ErrorHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorHandler.swift */; };
BA798837CAF824FACE6DDC91 /* ErrorHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorHandler.swift */; };
BA7988926836681D75A2D44C /* ErrorHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorHandler.swift */; };
BA798D222B6284F771465C87 /* ErrorHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorHandler.swift */; };
BA798DDD639F9AAFCF98EF39 /* ErrorHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA7983C1B85B8AE9BAF59E3E /* ErrorHandler.swift */; };
E6ED61311E27CD630058ACE9 /* UnauthorizedCallbackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6ED61301E27CD630058ACE9 /* UnauthorizedCallbackTests.swift */; };
E6ED61321E27CD630058ACE9 /* UnauthorizedCallbackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6ED61301E27CD630058ACE9 /* UnauthorizedCallbackTests.swift */; };
E6ED61331E27CD630058ACE9 /* UnauthorizedCallbackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6ED61301E27CD630058ACE9 /* UnauthorizedCallbackTests.swift */; };
Expand Down Expand Up @@ -247,6 +255,7 @@
44DFD39B1D9A2D5A0014E9F2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
44DFD39F1D9A2EA70014E9F2 /* CellData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CellData.swift; sourceTree = "<group>"; };
44DFD3A11D9A33610014E9F2 /* FakeImageController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeImageController.swift; sourceTree = "<group>"; };
BA7983C1B85B8AE9BAF59E3E /* ErrorHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorHandler.swift; sourceTree = "<group>"; };
E6ED61301E27CD630058ACE9 /* UnauthorizedCallbackTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnauthorizedCallbackTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -380,6 +389,7 @@
4488DBC01D9BBFCF007D7A14 /* FakeRequest.swift */,
1474C1C21CA2988900E15D84 /* TestCheck.swift */,
446C24621DFEE964006FA6CC /* Helpers.swift */,
BA7983C1B85B8AE9BAF59E3E /* ErrorHandler.swift */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -815,6 +825,7 @@
1413F4771CE7968E00482096 /* JSON.swift in Sources */,
4488DBCC1D9BCA0E007D7A14 /* FakeRequestTests.swift in Sources */,
445F053321007512003906C2 /* NetworkingTests.swift in Sources */,
BA798837CAF824FACE6DDC91 /* ErrorHandler.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -834,6 +845,7 @@
14638D0A1CC64629002B9433 /* Networking.swift in Sources */,
4488DBC51D9BBFCF007D7A14 /* FakeRequest.swift in Sources */,
14638D0B1CC64629002B9433 /* TestCheck.swift in Sources */,
BA7985EE02F8B15CAE1C86DD /* ErrorHandler.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -853,6 +865,7 @@
14638D201CC64733002B9433 /* Networking.swift in Sources */,
4488DBC61D9BBFCF007D7A14 /* FakeRequest.swift in Sources */,
14638D211CC64733002B9433 /* TestCheck.swift in Sources */,
BA79866331D2E147C9A9EE21 /* ErrorHandler.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -872,6 +885,7 @@
14638D361CC647B8002B9433 /* Networking.swift in Sources */,
4488DBC71D9BBFCF007D7A14 /* FakeRequest.swift in Sources */,
14638D371CC647B8002B9433 /* TestCheck.swift in Sources */,
BA798D222B6284F771465C87 /* ErrorHandler.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -891,6 +905,7 @@
14638D4C1CC64829002B9433 /* Networking.swift in Sources */,
4488DBC81D9BBFCF007D7A14 /* FakeRequest.swift in Sources */,
14638D4D1CC64829002B9433 /* TestCheck.swift in Sources */,
BA79878BCE133FA5429E6DAA /* ErrorHandler.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -926,6 +941,7 @@
1474C1C81CA2988900E15D84 /* Networking.swift in Sources */,
4488DBCA1D9BCA0E007D7A14 /* FakeRequestTests.swift in Sources */,
445F053121007512003906C2 /* NetworkingTests.swift in Sources */,
BA7988926836681D75A2D44C /* ErrorHandler.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -961,6 +977,7 @@
44B5D3A01D19B9E5004378B9 /* Networking.swift in Sources */,
4488DBCB1D9BCA0E007D7A14 /* FakeRequestTests.swift in Sources */,
445F053221007512003906C2 /* NetworkingTests.swift in Sources */,
BA79824C3FD10818DA976F8A /* ErrorHandler.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -984,6 +1001,7 @@
446C24631DFEE964006FA6CC /* Helpers.swift in Sources */,
44DFD3A01D9A2EA70014E9F2 /* CellData.swift in Sources */,
44A6D5481E4122DE00405A7E /* Networking+Private.swift in Sources */,
BA798DDD639F9AAFCF98EF39 /* ErrorHandler.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
30 changes: 30 additions & 0 deletions Sources/ErrorHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
////////////////////////////////////////////////////////////////////////////////
//
// SYMBIOSE
// Copyright 2020 Symbiose Inc
// All Rights Reserved.
//
// NOTICE: This software is proprietary information.
// Unauthorized use is prohibited.
//
////////////////////////////////////////////////////////////////////////////////

import Foundation

public protocol ErrorHandler {

func log(_ message: String)

func flush()

}

class ConsoleErrorHandler : ErrorHandler {

func log(_ message: String) {
print(message)
}

func flush() {}

}
61 changes: 31 additions & 30 deletions Sources/Networking+Private.swift
Original file line number Diff line number Diff line change
Expand Up @@ -348,31 +348,31 @@ extension Networking {
guard isErrorLoggingEnabled else { return }
guard let error = error else { return }

print(" ")
print("========== Networking Error ==========")
print(" ")
errorHandler.log(" ")
errorHandler.log("========== Networking Error ==========")
errorHandler.log(" ")

let isCancelled = error.code == NSURLErrorCancelled
if isCancelled {
if let request = request, let url = request.url {
print("Cancelled request: \(url.absoluteString)")
print(" ")
errorHandler.log("Cancelled request: \(url.absoluteString)")
errorHandler.log(" ")
}
} else {
print("*** Request ***")
print(" ")
errorHandler.log("*** Request ***")
errorHandler.log(" ")

print("Error \(error.code): \(error.description)")
print(" ")
errorHandler.log("Error \(error.code): \(error.description)")
errorHandler.log(" ")

if let request = request, let url = request.url {
print("URL: \(url.absoluteString)")
print(" ")
errorHandler.log("URL: \(url.absoluteString)")
errorHandler.log(" ")
}

if let headers = request?.allHTTPHeaderFields {
print("Headers: \(headers)")
print(" ")
errorHandler.log("Headers: \(headers)")
errorHandler.log(" ")
}

if let parameterType = parameterType, let parameters = parameters {
Expand All @@ -382,44 +382,45 @@ extension Networking {
let data = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
let string = String(data: data, encoding: .utf8)
if let string = string {
print("Parameters: \(string)")
print(" ")
errorHandler.log("Parameters: \(string)")
errorHandler.log(" ")
}
} catch let error as NSError {
print("Failed pretty printing parameters: \(parameters), error: \(error)")
print(" ")
errorHandler.log("Failed pretty printing parameters: \(parameters), error: \(error)")
errorHandler.log(" ")
}
case .formURLEncoded:
guard let parametersDictionary = parameters as? [String: Any] else { fatalError("Couldn't cast parameters as dictionary: \(parameters)") }
do {
let formattedParameters = try parametersDictionary.urlEncodedString()
print("Parameters: \(formattedParameters)")
errorHandler.log("Parameters: \(formattedParameters)")
} catch let error as NSError {
print("Failed parsing Parameters: \(parametersDictionary)\(error)")
errorHandler.log("Failed parsing Parameters: \(parametersDictionary)\(error)")
}
print(" ")
errorHandler.log(" ")
default: break
}
}

if let data = data, let stringData = String(data: data, encoding: .utf8) {
print("Data: \(stringData)")
print(" ")
errorHandler.log("Data: \(stringData)")
errorHandler.log(" ")
}

if let response = response as? HTTPURLResponse {
print("*** Response ***")
print(" ")
errorHandler.log("*** Response ***")
errorHandler.log(" ")

print("Headers: \(response.allHeaderFields)")
print(" ")
errorHandler.log("Headers: \(response.allHeaderFields)")
errorHandler.log(" ")

print("Status code: \(response.statusCode)\(HTTPURLResponse.localizedString(forStatusCode: response.statusCode))")
print(" ")
errorHandler.log("Status code: \(response.statusCode)\(HTTPURLResponse.localizedString(forStatusCode: response.statusCode))")
errorHandler.log(" ")
}
}
print("================= ~ ==================")
print(" ")
errorHandler.log("================= ~ ==================")
errorHandler.log(" ")
errorHandler.flush()
}

func cacheOrPurgeJSON(object: Any?, path: String, cacheName: String?, cachingLevel: CachingLevel) throws {
Expand Down
2 changes: 2 additions & 0 deletions Sources/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ open class Networking {
URLSession(configuration: self.configuration)
}()

public var errorHandler: ErrorHandler = ConsoleErrorHandler()

/// Caching options
public enum CachingLevel {
case memory
Expand Down

0 comments on commit b80476d

Please sign in to comment.