-
Notifications
You must be signed in to change notification settings - Fork 35
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
Surface specific XPC & login item errors #819
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1b43462
Add known failure store
quanganhdo f4b4104
Merge branch 'main' into anh/netp/error-ui
quanganhdo 5f73572
Add support for ClientError 5
quanganhdo 435b156
Fix Swiftlint
quanganhdo f8bff07
Move versionMismatched error out of BSK
quanganhdo 178f051
Merge branch 'main' into anh/netp/error-ui
quanganhdo e200aab
Fix lastKnownFailure setting
quanganhdo 019efc5
Merge branch 'main' into anh/netp/error-ui
quanganhdo 26700ba
Exclude iOS
quanganhdo 20ed11c
Platform specific update
quanganhdo d2a9125
Fix unit tests
quanganhdo d1dafa6
Fix unit tests
quanganhdo 2d37fc1
Merge branch 'main' into anh/netp/error-ui
quanganhdo b94a091
Remove localizedDescription
quanganhdo 0d077dd
Merge branch 'main' into anh/netp/error-ui
quanganhdo 25d0760
Merge branch 'main' into anh/netp/error-ui
quanganhdo 5267365
Merge branch 'main' into anh/netp/error-ui
quanganhdo 24af572
Draft
quanganhdo 997877c
Remove debug command
quanganhdo 6f09c82
Draft
quanganhdo c863703
Add tests and address PR comments
quanganhdo ab0ad2b
Merge branch 'main' into anh/netp/error-ui
quanganhdo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// KnownFailure.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol SilentErrorConvertible: Error { | ||
var asSilentError: KnownFailure.SilentError? { get } | ||
} | ||
|
||
@objc | ||
final public class KnownFailure: NSObject, Codable { | ||
public typealias SilentErrorCode = Int | ||
|
||
public enum SilentError: SilentErrorCode { | ||
case operationNotPermitted | ||
case loginItemVersionMismatched | ||
case registeredServerFetchingFailed | ||
} | ||
|
||
public let error: SilentErrorCode | ||
|
||
public init?(_ error: Error?) { | ||
if let nsError = error as? NSError, nsError.domain == "SMAppServiceErrorDomain", nsError.code == 1 { | ||
self.error = SilentError.operationNotPermitted.rawValue | ||
return | ||
} | ||
|
||
if let error = error as? SilentErrorConvertible, let silentError = error.asSilentError { | ||
self.error = silentError.rawValue | ||
return | ||
} | ||
|
||
return nil | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
Sources/NetworkProtection/Status/KnownFailureObserver/KnownFailureObserver.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// KnownFailureObserver.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import NetworkExtension | ||
|
||
public protocol KnownFailureObserver { | ||
var publisher: AnyPublisher<KnownFailure?, Never> { get } | ||
var recentValue: KnownFailure? { get } | ||
} |
60 changes: 60 additions & 0 deletions
60
...ion/Status/KnownFailureObserver/KnownFailureObserverThroughDistributedNotifications.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// KnownFailureObserverThroughDistributedNotifications.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#if os(macOS) | ||
|
||
import Combine | ||
import Foundation | ||
import NetworkExtension | ||
import NotificationCenter | ||
|
||
public class KnownFailureObserverThroughDistributedNotifications: KnownFailureObserver { | ||
public lazy var publisher = subject.eraseToAnyPublisher() | ||
public var recentValue: KnownFailure? { | ||
subject.value | ||
} | ||
|
||
private let subject = CurrentValueSubject<KnownFailure?, Never>(nil) | ||
|
||
private let distributedNotificationCenter: DistributedNotificationCenter | ||
private var cancellable: AnyCancellable? | ||
|
||
public init(distributedNotificationCenter: DistributedNotificationCenter = .default()) { | ||
self.distributedNotificationCenter = distributedNotificationCenter | ||
|
||
start() | ||
} | ||
|
||
private func start() { | ||
cancellable = distributedNotificationCenter.publisher(for: .knownFailureUpdated).sink { [weak self] notification in | ||
self?.handleKnownFailureUpdated(notification) | ||
} | ||
} | ||
|
||
private func handleKnownFailureUpdated(_ notification: Notification) { | ||
if let object = notification.object as? String, | ||
let data = object.data(using: .utf8), | ||
let failure = try? JSONDecoder().decode(KnownFailure.self, from: data) { | ||
subject.send(failure) | ||
} else { | ||
subject.send(nil) | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
Sources/NetworkProtection/Storage/NetworkProtectionKnownFailureStore.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// NetworkProtectionKnownFailureStore.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import Common | ||
|
||
final public class NetworkProtectionKnownFailureStore { | ||
private static let lastKnownFailureKey = "com.duckduckgo.NetworkProtectionKnownFailureStore.knownFailure" | ||
private let userDefaults: UserDefaults | ||
|
||
#if os(macOS) | ||
private let distributedNotificationCenter: DistributedNotificationCenter | ||
|
||
public init(userDefaults: UserDefaults = .standard, | ||
distributedNotificationCenter: DistributedNotificationCenter = .default()) { | ||
self.userDefaults = userDefaults | ||
self.distributedNotificationCenter = distributedNotificationCenter | ||
} | ||
#else | ||
public init(userDefaults: UserDefaults = .standard) { | ||
self.userDefaults = userDefaults | ||
} | ||
#endif | ||
|
||
public var lastKnownFailure: KnownFailure? { | ||
get { | ||
guard let data = userDefaults.data(forKey: Self.lastKnownFailureKey) else { return nil } | ||
return try? JSONDecoder().decode(KnownFailure.self, from: data) | ||
} | ||
|
||
set { | ||
if let newValue, let data = try? JSONEncoder().encode(newValue) { | ||
userDefaults.set(data, forKey: Self.lastKnownFailureKey) | ||
#if os(macOS) | ||
postKnownFailureUpdatedNotification(data: data) | ||
#endif | ||
} else { | ||
userDefaults.removeObject(forKey: Self.lastKnownFailureKey) | ||
#if os(macOS) | ||
postKnownFailureUpdatedNotification() | ||
#endif | ||
} | ||
} | ||
} | ||
|
||
public func reset() { | ||
lastKnownFailure = nil | ||
} | ||
|
||
// MARK: - Posting Notifications | ||
|
||
#if os(macOS) | ||
private func postKnownFailureUpdatedNotification(data: Data? = nil) { | ||
let object: String? = { | ||
guard let data else { return nil } | ||
return String(data: data, encoding: .utf8) | ||
}() | ||
distributedNotificationCenter.post(.knownFailureUpdated, object: object) | ||
} | ||
#endif | ||
} |
30 changes: 30 additions & 0 deletions
30
Sources/NetworkProtectionTestUtils/Status/MockKnownFailureObserver.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// MockKnownFailureObserver.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import NetworkProtection | ||
|
||
public final class MockKnownFailureObserver: KnownFailureObserver { | ||
public init() {} | ||
public let subject = CurrentValueSubject<KnownFailure?, Never>(nil) | ||
public lazy var publisher = subject.eraseToAnyPublisher() | ||
public var recentValue: KnownFailure? { | ||
subject.value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This forces all errors that are "known" to be defined in BSK which seems unnecessarily restrictive.
Why instead not just make the error adhere to
Codable
and store that inNetworkProtectionKnownFailureStore
.Then the VPN app can decide what errors are worth passing over IPC, but at least we don't have to add errors that are macOS specific to BSK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's tricky as we need a concrete type to decode the error; and that type needs to be passed around in different ways (e.g. as part of
XPCClientInterface
, distributed notification object).To keep it simple, I've moved the custom handling code for
TunnelError
outside of the initializer and instead made it so that an error conforming toInternalErrorWrapping
can expose its internal error. That way we can keep specific errors outside of BSK.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood.
That said, I do think that instead of encoding domain, code and localizedDescription, we should turn this into a
Codable
enum. See this comment for context.