diff --git a/src/flows/FlowBridge.swift b/src/flows/FlowBridge.swift index fa325d9..a770f40 100644 --- a/src/flows/FlowBridge.swift +++ b/src/flows/FlowBridge.swift @@ -95,7 +95,11 @@ extension FlowBridge: WKScriptMessageHandler { delegate?.bridgeDidReceiveRequest(self, request: request) case .failure: logger(.error, "Bridge received failure event", message.body) - delegate?.bridgeDidFailAuthentication(self, error: DescopeError.flowFailed.with(message: "Unexpected authentication failure [\(message.body)]")) + if let dict = message.body as? [String: Any], let error = DescopeError(errorResponse: dict) { + delegate?.bridgeDidFailAuthentication(self, error: error) + } else { + delegate?.bridgeDidFailAuthentication(self, error: DescopeError.flowFailed.with(message: "Unexpected authentication failure")) + } case .success: logger(.info, "Bridge received success event") guard let json = message.body as? String, case let data = Data(json.utf8) else { @@ -148,7 +152,7 @@ extension FlowBridge: WKNavigationDelegate { // Don't print an error log if this was triggered by a non-2xx status code that was caught // above and causing the delegate function to return `.cancel`. We rely on the coordinator // to not notify about errors multiple times. - if case let error = error as NSError, error.domain == "WebKitErrorDomain", error.code == 102 { // https://developer.apple.com/documentation/webkit/1569748-webkit_loading_fail_enumeration_/webkiterrorframeloadinterruptedbypolicychange + if case let error = error as NSError, error.domain == "WebKitErrorDomain", error.code == 102 { // https://chromium.googlesource.com/chromium/src/+/2233628f5f5b32c7b458428f8d5cfbd0a18be82e/ios/web/public/web_kit_constants.h#25 logger(.info, "Webview loading was cancelled") } else { logger(.error, "Webview failed loading url", error) diff --git a/src/internal/http/ClientErrors.swift b/src/internal/http/ClientErrors.swift index 826db24..22f4f75 100644 --- a/src/internal/http/ClientErrors.swift +++ b/src/internal/http/ClientErrors.swift @@ -6,6 +6,10 @@ import Foundation extension DescopeError { init?(errorResponse data: Data) { guard let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return nil } + self.init(errorResponse: dict) + } + + init?(errorResponse dict: [String: Any]) { guard let code = dict["errorCode"] as? String else { return nil } var desc = "Descope server error" // is always supposed to be overwritten below if let value = dict["errorDescription"] as? String, !value.isEmpty { diff --git a/src/internal/others/Internal.swift b/src/internal/others/Internal.swift index c4beb43..8cffdb9 100644 --- a/src/internal/others/Internal.swift +++ b/src/internal/others/Internal.swift @@ -27,9 +27,7 @@ extension DescopeLogger? { extension Data { init?(base64URLEncoded base64URLString: String, options: Base64DecodingOptions = []) { - var str = base64URLString - .replacingOccurrences(of: "-", with: "+") - .replacingOccurrences(of: "_", with: "/") + var str = base64URLString.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/") if str.count % 4 > 0 { str.append(String(repeating: "=", count: 4 - str.count % 4)) } diff --git a/src/types/Error.swift b/src/types/Error.swift index dbd4dc0..31b5ddf 100644 --- a/src/types/Error.swift +++ b/src/types/Error.swift @@ -148,7 +148,7 @@ extension DescopeError: LocalizedError { public var errorDescription: String? { var str = "\(desc)" if let cause = cause as? NSError { - str += ": \(cause.localizedDescription) [\(code): \(cause.code)]" + str += ": \(cause.localizedDescription) [\(cause.code)]" } else if let message { str += ": \(message) [\(code)]" } else { diff --git a/src/types/Others.swift b/src/types/Others.swift index 97b7591..996d9f1 100644 --- a/src/types/Others.swift +++ b/src/types/Others.swift @@ -1,8 +1,5 @@ import Foundation -#if os(iOS) -import UIKit -#endif /// The delivery method for an OTP or Magic Link message. public enum DeliveryMethod: String, Sendable {