Skip to content

When to warn users when an error happens?

André Herculano edited this page Aug 20, 2020 · 1 revision

The problem

It's a good practice to give the user a feedback when something unexpected happens. In the GDPRConsentViewController there are many moving parts and as with any piece of software that communicates with the external world, unexpected things happens all the time. In this wiki we describe an approach (developed together with @s-hintz) on deciding when you should warn users about an error and when not.

Long story short, you want to bother the user with an error message only if the consent UI was presented or when custom setting consent to a vendor (when using the customConsentTo).

The solution

So the idea is to keep a flag, let's name it shouldWarnUserIfError that will indicate if the user should be warned or not if an error happens (onError is called).

This flag would be set to true in 2 places:

  1. inside onConsentUIWillShow
  2. every time you call customConsentTo

And would be reset to false in 3 spots:

  1. inside onConsentReady
  2. inside onError
  3. inside the completion handler you pass to customConsentTo method

Example

class MyViewController: UIViewController {
  var shouldWarnUserIfError = false
  var consentViewController: GDPRConsentViewController = { /* initialise SDK */ }

  func enableVendor(withId vendorId: String) {
    shouldWarnUserIfError = true
    consentViewController.customConsentTo(vendors: [vendorId], categories: [], legIntCategories: []) {
      // successful response from customConsentTo
      shouldWarnUserIfError = false
    }
  }
}

extension MyViewController: GDPRConsentDelegate {
  func gdprConsentUIWillShow() {
    shouldWarnUserIfError = true
    // rest of implementation
  }

  func onConsentReady(gdprUUID: GDPRUUID, userConsent: GDPRUserConsent) {
    shouldWarnUserIfError = false
    // rest of implementation
  }

  func onError(error: GDPRConsentViewControllerError?) {
    if shouldWarnUserIfError {
      // warn the user about what happened
    }
    // check if consent ui is presented and remove it
    shouldWarnUserIfError = false
  }
}