Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling when flow execution encounters an error #78

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/flows/FlowBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/internal/http/ClientErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions src/internal/others/Internal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions src/types/Others.swift
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down