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

Add additonal error handling to DBP database and related classes #2384

Merged
merged 31 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fb20e20
Change Feature.pushAction assert failure to a fatal error
THISISDINOSAUR Mar 7, 2024
b4e525d
Add error handling to Database
THISISDINOSAUR Mar 11, 2024
7e9b11c
Add error handling to DataManager
THISISDINOSAUR Mar 11, 2024
f582ffe
Add error handling to BrokerUpdater
THISISDINOSAUR Mar 11, 2024
16b5efe
Change UICommunicationLayer methods to throw errors instead of return…
THISISDINOSAUR Mar 11, 2024
499b461
Update classes to handle DB errors (either temporarily or permanently…
THISISDINOSAUR Mar 11, 2024
e4469ce
Add os logging back to Database
THISISDINOSAUR Mar 13, 2024
edb95e9
Add inert SecureVault error reporter to DBP database
THISISDINOSAUR Mar 13, 2024
2a4d0cb
Fix rogue var
THISISDINOSAUR Mar 13, 2024
fa24c7e
Remove TODO
THISISDINOSAUR Mar 13, 2024
3319885
Add various os_log ing for errors
THISISDINOSAUR Mar 13, 2024
6fb5b8a
Add custom error for if data is not in DB
THISISDINOSAUR Mar 13, 2024
40f3dff
Fix various linting issues
THISISDINOSAUR Mar 13, 2024
dcd1756
Merge branch 'main' into elle/expand-dbp-error-handling
THISISDINOSAUR Mar 13, 2024
70a518a
Fix weird db file issue
THISISDINOSAUR Mar 13, 2024
8dc1c2e
Add appropriate tries to new methods from merge
THISISDINOSAUR Mar 13, 2024
8a9f526
Add throwing to fileResources
THISISDINOSAUR Mar 13, 2024
0022fc8
Refine os_log messages
THISISDINOSAUR Mar 13, 2024
827f0af
Refine more os log messages
THISISDINOSAUR Mar 13, 2024
8b6bc30
Make DBP tests have same swift lint rules as other tests
THISISDINOSAUR Mar 14, 2024
df2e53f
Change DBP tests to use try! instead of try?
THISISDINOSAUR Mar 14, 2024
5cacc1f
Restore DBPFeature assertionFailure
THISISDINOSAUR Mar 26, 2024
676a402
Expand dbp error handling iteration 2: further up the stack + pixels …
THISISDINOSAUR Apr 2, 2024
8ff1bbb
Merge branch 'main' into elle/expand-dbp-error-handling
THISISDINOSAUR Apr 2, 2024
0573d21
Delete DuckDuckGo/Subscription/SubscriptionFeatureAvailability.swift
THISISDINOSAUR Apr 2, 2024
387fad0
Apply suggestions from code review
THISISDINOSAUR Apr 2, 2024
839e06e
Resolve merge issues
THISISDINOSAUR Apr 2, 2024
21262aa
Merge branch 'elle/expand-dbp-error-handling' of github.com:duckduckg…
THISISDINOSAUR Apr 2, 2024
468cd25
Add additional method to operations collection delegate
THISISDINOSAUR Apr 2, 2024
ff20faf
Add new DBP error catagory for DB errors
THISISDINOSAUR Apr 2, 2024
47f3f31
Modify DBP DataManager interface to make cache setting more explicit …
THISISDINOSAUR Apr 8, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"location" : "https://github.com/apple/swift-syntax",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this again, Brindy has a task to fix this - we can leave it this way until that happens tbh.

"state" : {
"revision" : "64889f0c732f210a935a0ad7cda38f77f876262d",
"version" : "509.1.1"
Expand Down
17 changes: 14 additions & 3 deletions DuckDuckGo/DBP/DBPHomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public extension Notification.Name {
final class DBPHomeViewController: NSViewController {
private var presentedWindowController: NSWindowController?
private let dataBrokerProtectionManager: DataBrokerProtectionManager
private let pixelHandler: EventMapping<DataBrokerProtectionPixels> = DataBrokerProtectionPixelsHandler()

lazy var dataBrokerProtectionViewController: DataBrokerProtectionViewController = {
let privacyConfigurationManager = PrivacyFeatures.contentBlocking.privacyConfigurationManager
Expand Down Expand Up @@ -83,9 +84,14 @@ final class DBPHomeViewController: NSViewController {
attachDataBrokerContainerView()
}

if dataBrokerProtectionManager.dataManager.fetchProfile() != nil {
let dbpDateStore = DefaultWaitlistActivationDateStore(source: .dbp)
dbpDateStore.updateLastActiveDate()
do {
if try dataBrokerProtectionManager.dataManager.fetchProfile() != nil {
let dbpDateStore = DefaultWaitlistActivationDateStore(source: .dbp)
dbpDateStore.updateLastActiveDate()
}
samsymons marked this conversation as resolved.
Show resolved Hide resolved
} catch {
os_log("DBPHomeViewController error: viewDidLoad, error: %{public}@", log: .error, error.localizedDescription)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably worth us renaming these log objects to something that specifies that they're DBP related at some point. At first glance I thought this wasn't using a log object at all and the info level was error, but actually this is a default level log with an custom log object. Not confusing at all 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah definitely, outside of the DBP package it's completely confusing

pixelHandler.fire(.generalError(error: error, functionOccurredIn: "DBPHomeViewController.viewDidLoad"))
}
}

Expand Down Expand Up @@ -146,6 +152,11 @@ public class DataBrokerProtectionPixelsHandler: EventMapping<DataBrokerProtectio
switch event {
case .error(let error, _):
Pixel.fire(.debug(event: .pixelKitEvent(event), error: error))
case .generalError(let error, _),
.secureVaultInitError(let error),
.secureVaultError(let error):
// We can't use .debug directly because it modifies the pixel name and clobbers the params
Pixel.fire(.pixelKitEvent(DebugEvent(event, error: error)))
case .ipcServerOptOutAllBrokersCompletion(error: let error),
.ipcServerScanAllBrokersCompletion(error: let error),
.ipcServerRunQueuedOperationsCompletion(error: let error):
Expand Down
4 changes: 2 additions & 2 deletions DuckDuckGo/DBP/DataBrokerProtectionAppEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ struct DataBrokerProtectionAppEvents {

// If we don't have profileQueries it means there's no user profile saved in our DB
// In this case, let's disable the agent and delete any left-over data because there's nothing for it to do
let profileQueries = await DataBrokerProtectionManager.shared.dataManager.fetchBrokerProfileQueryData(ignoresCache: true)
if profileQueries.count > 0 {
if let profileQueries = try? DataBrokerProtectionManager.shared.dataManager.fetchBrokerProfileQueryData(ignoresCache: true),
profileQueries.count > 0 {
restartBackgroundAgent(loginItemsManager: loginItemsManager)
} else {
featureVisibility.disableAndDeleteForWaitlistUsers()
Expand Down
36 changes: 27 additions & 9 deletions DuckDuckGo/DBP/DataBrokerProtectionDebugMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,15 @@ final class DataBrokerProtectionDebugMenu: NSMenu {
os_log("Running queued operations...", log: .dataBrokerProtection)
let showWebView = sender.representedObject as? Bool ?? false

DataBrokerProtectionManager.shared.scheduler.runQueuedOperations(showWebView: showWebView) { error in
if let error = error {
os_log("Queued operations finished, error: %{public}@", log: .dataBrokerProtection, error.localizedDescription)
DataBrokerProtectionManager.shared.scheduler.runQueuedOperations(showWebView: showWebView) { errors in
if let errors = errors {
if let oneTimeError = errors.oneTimeError {
os_log("Queued operations finished, error: %{public}@", log: .dataBrokerProtection, oneTimeError.localizedDescription)
}
if let operationErrors = errors.operationErrors,
operationErrors.count != 0 {
os_log("Queued operations finished, operation errors count: %{public}@", log: .dataBrokerProtection, operationErrors.count)
}
} else {
os_log("Queued operations finished", log: .dataBrokerProtection)
}
Expand All @@ -217,9 +223,15 @@ final class DataBrokerProtectionDebugMenu: NSMenu {
os_log("Running scan operations...", log: .dataBrokerProtection)
let showWebView = sender.representedObject as? Bool ?? false

DataBrokerProtectionManager.shared.scheduler.scanAllBrokers(showWebView: showWebView) { error in
if let error = error {
os_log("Scan operations finished, error: %{public}@", log: .dataBrokerProtection, error.localizedDescription)
DataBrokerProtectionManager.shared.scheduler.scanAllBrokers(showWebView: showWebView) { errors in
if let errors = errors {
if let oneTimeError = errors.oneTimeError {
os_log("scan operations finished, error: %{public}@", log: .dataBrokerProtection, oneTimeError.localizedDescription)
}
if let operationErrors = errors.operationErrors,
operationErrors.count != 0 {
os_log("scan operations finished, operation errors count: %{public}@", log: .dataBrokerProtection, operationErrors.count)
}
} else {
os_log("Scan operations finished", log: .dataBrokerProtection)
}
Expand All @@ -230,9 +242,15 @@ final class DataBrokerProtectionDebugMenu: NSMenu {
os_log("Running Optout operations...", log: .dataBrokerProtection)
let showWebView = sender.representedObject as? Bool ?? false

DataBrokerProtectionManager.shared.scheduler.optOutAllBrokers(showWebView: showWebView) { error in
if let error = error {
os_log("Optout operations finished, error: %{public}@", log: .dataBrokerProtection, error.localizedDescription)
DataBrokerProtectionManager.shared.scheduler.optOutAllBrokers(showWebView: showWebView) { errors in
if let errors = errors {
if let oneTimeError = errors.oneTimeError {
os_log("Optout operations finished, error: %{public}@", log: .dataBrokerProtection, oneTimeError.localizedDescription)
}
if let operationErrors = errors.operationErrors,
operationErrors.count != 0 {
os_log("Optout operations finished, operation errors count: %{public}@", log: .dataBrokerProtection, operationErrors.count)
}
} else {
os_log("Optout operations finished", log: .dataBrokerProtection)
}
Expand Down
6 changes: 5 additions & 1 deletion DuckDuckGo/DBP/DataBrokerProtectionFeatureDisabler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ struct DataBrokerProtectionFeatureDisabler: DataBrokerProtectionFeatureDisabling

scheduler.disableLoginItem()

dataManager.removeAllData()
do {
try dataManager.removeAllData()
} catch {
os_log("DataBrokerProtectionFeatureDisabler error: disableAndDelete, error: %{public}@", log: .error, error.localizedDescription)
}
Comment on lines +49 to +53
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forget, do we need a pixel here? (feels like if we started failing to remove data broker data, that could be a big deal and we would definitely want to know about it)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have one for if removing all data failures certainly. The actual method that is called that can fail here is database.deleteProfileData, which will fire thusly:

pixelHandler.fire(.generalError(error: error, functionOccurredIn: "DataBrokerProtectionDatabase.deleteProfileData"))

So then I think it comes down to two questions:

  1. Do we want a separate pixel on the higher level of FeatureDisabler?
    I lean no on this
  2. Do we want a separate pixel for grafana purposes?
    I think the general answer to this is yes, but that applies to the entire lot of dbp error pixels which we need to do, and sounds like we might do soon, so this leads me to a third question:
  3. Should we make an exception for this one to get the data more in the short term?
    Maybe? I'm not sure tbh


DataBrokerProtectionLoginItemPixels.fire(pixel: .dataBrokerDisableAndDeleteDaily, frequency: .dailyOnly)
NotificationCenter.default.post(name: .dbpWasDisabled, object: nil)
Expand Down
9 changes: 6 additions & 3 deletions DuckDuckGo/DBP/DataBrokerProtectionLoginItemScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ extension DataBrokerProtectionLoginItemScheduler: DataBrokerProtectionScheduler
ipcScheduler.statusPublisher
}

func scanAllBrokers(showWebView: Bool, completion: ((Error?) -> Void)?) {
func scanAllBrokers(showWebView: Bool,
completion: ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)?) {
enableLoginItem()
ipcScheduler.scanAllBrokers(showWebView: showWebView, completion: completion)
}
Expand All @@ -69,15 +70,17 @@ extension DataBrokerProtectionLoginItemScheduler: DataBrokerProtectionScheduler
ipcScheduler.stopScheduler()
}

func optOutAllBrokers(showWebView: Bool, completion: ((Error?) -> Void)?) {
func optOutAllBrokers(showWebView: Bool,
completion: ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)?) {
ipcScheduler.optOutAllBrokers(showWebView: showWebView, completion: completion)
}

func runAllOperations(showWebView: Bool) {
ipcScheduler.runAllOperations(showWebView: showWebView)
}

func runQueuedOperations(showWebView: Bool, completion: ((Error?) -> Void)?) {
func runQueuedOperations(showWebView: Bool,
completion: ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)?) {
ipcScheduler.runQueuedOperations(showWebView: showWebView, completion: completion)
}
}
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/DBP/DataBrokerProtectionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class DataBrokerProtectionManager {
private let dataBrokerProtectionWaitlistDataSource: WaitlistActivationDateStore = DefaultWaitlistActivationDateStore(source: .dbp)

lazy var dataManager: DataBrokerProtectionDataManager = {
let dataManager = DataBrokerProtectionDataManager(fakeBrokerFlag: fakeBrokerFlag)
let dataManager = DataBrokerProtectionDataManager(pixelHandler: pixelHandler, fakeBrokerFlag: fakeBrokerFlag)
dataManager.delegate = self
return dataManager
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class DataBrokerProtectionBackgroundManager {
private lazy var ipcServiceManager = IPCServiceManager(scheduler: scheduler, pixelHandler: pixelHandler)

lazy var dataManager: DataBrokerProtectionDataManager = {
DataBrokerProtectionDataManager(fakeBrokerFlag: fakeBrokerFlag)
DataBrokerProtectionDataManager(pixelHandler: pixelHandler, fakeBrokerFlag: fakeBrokerFlag)
}()

lazy var scheduler: DataBrokerProtectionScheduler = {
Expand Down Expand Up @@ -78,19 +78,36 @@ public final class DataBrokerProtectionBackgroundManager {
public func runOperationsAndStartSchedulerIfPossible() {
pixelHandler.fire(.backgroundAgentRunOperationsAndStartSchedulerIfPossible)

// If there's no saved profile we don't need to start the scheduler
if dataManager.fetchProfile() != nil {
scheduler.runQueuedOperations(showWebView: false) { [weak self] error in
guard error == nil else {
// Ideally we'd fire a pixel here, however at the moment the scheduler never ever returns an error
return
}
do {
// If there's no saved profile we don't need to start the scheduler
guard (try dataManager.fetchProfile()) != nil else {
pixelHandler.fire(.backgroundAgentRunOperationsAndStartSchedulerIfPossibleNoSavedProfile)
return
}
} catch {
pixelHandler.fire(.generalError(error: error,
functionOccurredIn: "DataBrokerProtectionBackgroundManager.runOperationsAndStartSchedulerIfPossible"))
return
}

self?.pixelHandler.fire(.backgroundAgentRunOperationsAndStartSchedulerIfPossibleRunQueuedOperationsCallbackStartScheduler)
self?.scheduler.startScheduler()
scheduler.runQueuedOperations(showWebView: false) { [weak self] errors in
if let errors = errors {
if let oneTimeError = errors.oneTimeError {
os_log("Error during BackgroundManager runOperationsAndStartSchedulerIfPossible in scheduler.runQueuedOperations(), error: %{public}@",
log: .dataBrokerProtection,
oneTimeError.localizedDescription)
self?.pixelHandler.fire(.generalError(error: oneTimeError,
functionOccurredIn: "DataBrokerProtectionBackgroundManager.runOperationsAndStartSchedulerIfPossible"))
}
if let operationErrors = errors.operationErrors,
operationErrors.count != 0 {
os_log("Operation error(s) during BackgroundManager runOperationsAndStartSchedulerIfPossible in scheduler.runQueuedOperations(), count: %{public}d", log: .dataBrokerProtection, operationErrors.count)
}
return
}
} else {
pixelHandler.fire(.backgroundAgentRunOperationsAndStartSchedulerIfPossibleNoSavedProfile)

self?.pixelHandler.fire(.backgroundAgentRunOperationsAndStartSchedulerIfPossibleRunQueuedOperationsCallbackStartScheduler)
self?.scheduler.startScheduler()
}
}
}
27 changes: 15 additions & 12 deletions DuckDuckGoDBPBackgroundAgent/IPCServiceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,30 @@ extension IPCServiceManager: IPCServerInterface {
scheduler.stopScheduler()
}

func optOutAllBrokers(showWebView: Bool, completion: @escaping ((Error?) -> Void)) {
func optOutAllBrokers(showWebView: Bool,
completion: @escaping ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)) {
pixelHandler.fire(.ipcServerOptOutAllBrokers)
scheduler.optOutAllBrokers(showWebView: showWebView) { error in
self.pixelHandler.fire(.ipcServerOptOutAllBrokersCompletion(error: error))
completion(error)
scheduler.optOutAllBrokers(showWebView: showWebView) { errors in
self.pixelHandler.fire(.ipcServerOptOutAllBrokersCompletion(error: errors?.oneTimeError))
completion(errors)
}
}

func scanAllBrokers(showWebView: Bool, completion: @escaping ((Error?) -> Void)) {
func scanAllBrokers(showWebView: Bool,
completion: @escaping ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)) {
pixelHandler.fire(.ipcServerScanAllBrokers)
scheduler.scanAllBrokers(showWebView: showWebView) { error in
self.pixelHandler.fire(.ipcServerScanAllBrokersCompletion(error: error))
completion(error)
scheduler.scanAllBrokers(showWebView: showWebView) { errors in
self.pixelHandler.fire(.ipcServerScanAllBrokersCompletion(error: errors?.oneTimeError))
completion(errors)
}
}

func runQueuedOperations(showWebView: Bool, completion: @escaping ((Error?) -> Void)) {
func runQueuedOperations(showWebView: Bool,
completion: @escaping ((DataBrokerProtectionSchedulerErrorCollection?) -> Void)) {
pixelHandler.fire(.ipcServerRunQueuedOperations)
scheduler.runQueuedOperations(showWebView: showWebView) { error in
self.pixelHandler.fire(.ipcServerRunQueuedOperationsCompletion(error: error))
completion(error)
scheduler.runQueuedOperations(showWebView: showWebView) { errors in
self.pixelHandler.fire(.ipcServerRunQueuedOperationsCompletion(error: errors?.oneTimeError))
completion(errors)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public enum DataBrokerProtectionError: Error, Equatable, Codable {
case solvingCaptchaWithCallbackError
case cantCalculatePreferredRunDate
case httpError(code: Int)
case dataNotInDatabase

static func parse(params: Any) -> DataBrokerProtectionError {
let errorDataResult = try? JSONSerialization.data(withJSONObject: params)
Expand Down Expand Up @@ -88,6 +89,8 @@ extension DataBrokerProtectionError {
return "cantCalculatePreferredRunDate"
case .httpError:
return "httpError"
case .dataNotInDatabase:
return "dataNotInDatabase"
}
}
}
Loading
Loading