From 9b9b7c1176be773be2f91fdbfcda7cf345241ac1 Mon Sep 17 00:00:00 2001 From: Ashutosh Jena Date: Tue, 13 Feb 2024 13:34:23 +0530 Subject: [PATCH 1/5] FLA-1875Update Error Handling --- FinBoxLending/network/APIService.swift | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/FinBoxLending/network/APIService.swift b/FinBoxLending/network/APIService.swift index 692bd0d..20c392d 100644 --- a/FinBoxLending/network/APIService.swift +++ b/FinBoxLending/network/APIService.swift @@ -71,7 +71,7 @@ struct APIService { // Convert the response to object sessionResponse = try JSONDecoder().decode(SessionResponse.self, from: data) } catch { - self.handleClientError(completion: completion, error: error) + self.handleError(completion: completion, error: "JSON Error") } // Handle the HTTP response @@ -142,25 +142,36 @@ struct APIService { hidePoweredBy: userPref.hidePoweredBy, sdkType: "hybrid:ios:0.0.1") } + /// Creates SessionResult for Callback + /// - Parameters + /// - error (Optional) : String error + /// - sessionURL (Optional): String URL + /// - Returns + /// - SessionResult object with only error or sessionURL + func getResult(error: String?, sessionURL: String?) -> SessionResult { + if (error != nil && sessionURL == nil) { + return SessionResult(error: error, sessionURL: nil) + } else { + return SessionResult(error: nil, sessionURL: sessionURL) + } + } + /// Handles client errors func handleClientError(completion: @escaping (SessionResult) -> Void, error: Any) { debugPrint("Response Error Client: \(error as Any)") - let result = SessionResult(error: String(describing: error), sessionURL: nil) - sendCallback(completion: completion, result: result) + sendCallback(completion: completion, result: getResult(error: String(describing: error), sessionURL: nil)) } /// Handles server errors func handleServerError(completion: @escaping (SessionResult) -> Void, error: Any) { debugPrint("Response Error Server: \(String(describing: error))") - let result = SessionResult(error: String(describing: error), sessionURL: nil) - sendCallback(completion: completion, result: result) + sendCallback(completion: completion, result: getResult(error: String(describing: error), sessionURL: nil)) } /// Handles generic errors func handleError(completion: @escaping (SessionResult) -> Void, error: Any) { debugPrint("Response Error Generic: \(String(describing: error))") - let result = SessionResult(error: String(describing: error), sessionURL: nil) - sendCallback(completion: completion, result: result) + sendCallback(completion: completion, result: getResult(error: String(describing: error), sessionURL: nil)) } /// Sends an asynchronous callback after a (slight-possible) delay. From 2508dcf3be30442da0dd30705dcdaaa8425771f1 Mon Sep 17 00:00:00 2001 From: Ashutosh Jena Date: Tue, 13 Feb 2024 13:41:17 +0530 Subject: [PATCH 2/5] FLA-1876 Add CallbackSent struct --- FinBoxLending/network/CallbackSent.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 FinBoxLending/network/CallbackSent.swift diff --git a/FinBoxLending/network/CallbackSent.swift b/FinBoxLending/network/CallbackSent.swift new file mode 100644 index 0000000..81534e2 --- /dev/null +++ b/FinBoxLending/network/CallbackSent.swift @@ -0,0 +1,20 @@ +// +// CallbackSent.swift +// FinBoxLending +// +// Created by Ashutosh Jena on 13/02/24. +// + +import Foundation + +/// Struct to hold callback sent status +struct CallbackSent { + + /// Flag to hold status + var status = false + + /// Function which updates the sent status + mutating func updateStatus() { + status = true + } +} From 76539bded2909e105e4eba64e56f278fbf4a8b06 Mon Sep 17 00:00:00 2001 From: Ashutosh Jena Date: Tue, 13 Feb 2024 13:41:39 +0530 Subject: [PATCH 3/5] FLA-1876 Restrict to one callback --- FinBoxLending/network/APIService.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/FinBoxLending/network/APIService.swift b/FinBoxLending/network/APIService.swift index 20c392d..593dd24 100644 --- a/FinBoxLending/network/APIService.swift +++ b/FinBoxLending/network/APIService.swift @@ -10,6 +10,7 @@ import Foundation struct APIService { static let shared = APIService() + private static var callbackSent = CallbackSent() /// Constructs the URL for the session. /// - Returns: The URL for the session, formed by combining the `BASE_URL` and `END_POINT` @@ -179,6 +180,15 @@ struct APIService { /// - completion: A closure to be executed when the asynchronous operation completes. It takes a `String` parameter. /// - result: The result to be passed to the completion closure. func sendCallback(completion: @escaping (SessionResult) -> Void, result: SessionResult) { + // Check if the callback has already been sent + guard !APIService.callbackSent.status else { + return + } + + // Update the sent status + APIService.callbackSent.updateStatus() + + // Send callback DispatchQueue.global().asyncAfter(deadline: .now()) { completion(result) } From 9ef9fd740ba8b8412d9373d21a1b275a69dfdc26 Mon Sep 17 00:00:00 2001 From: Ashutosh Jena Date: Tue, 13 Feb 2024 15:33:04 +0530 Subject: [PATCH 4/5] FLA-1877 Close sdk on error --- FinBoxLending/LendingView.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/FinBoxLending/LendingView.swift b/FinBoxLending/LendingView.swift index 852004d..e0580bc 100644 --- a/FinBoxLending/LendingView.swift +++ b/FinBoxLending/LendingView.swift @@ -14,6 +14,8 @@ public struct LendingView: View { // Result Function public let lendingResult : ((FinBoxJourneyResult) -> Void) + @Environment(\.dismiss) private var dismiss + public init(lendingResult: @escaping (FinBoxJourneyResult) -> Void) { self.lendingResult = lendingResult } @@ -45,7 +47,8 @@ public struct LendingView: View { func handleError(error: String) -> some View { lendingResult(FinBoxJourneyResult(code: "", screen: "", message: error)) - return Text("\(String(describing: error))") + dismiss() + return EmptyView() } } From 5cb4bf243f3f02cf90c37b395dbf801665ddcfba Mon Sep 17 00:00:00 2001 From: Ashutosh Jena Date: Tue, 13 Feb 2024 15:33:58 +0530 Subject: [PATCH 5/5] FLA-1875 Send Localized Error Description --- FinBoxLending/network/APIService.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FinBoxLending/network/APIService.swift b/FinBoxLending/network/APIService.swift index 593dd24..adaf241 100644 --- a/FinBoxLending/network/APIService.swift +++ b/FinBoxLending/network/APIService.swift @@ -50,7 +50,7 @@ struct APIService { let task = URLSession.shared.dataTask(with: requestParams) { data, response, error in if let error = error { - self.handleError(completion: completion, error: error) + self.handleError(completion: completion, error: error.localizedDescription) return }