Share
Embed
Error handling made easy
UX doesn't only come down to looks and speed. Error handling is quite as important, and in order to get it right it has to be easy and straightforward. However, for most, it is still a mundane task with painfully too many cases to consider. In this talk we propose a recipe for reducing this friction and for adding complex error handling with just a few lines of code.
Presented in try!Swift New York in 2017
https://academy.realm.io/posts/try-swift-nyc-2017-eleni-papanikolopoulou-kostas-kremizas-error-handling/?fbclid=IwAR2ZrdxgN6gcPA3vNya7wOOYOgiM7Z4XtbtXJEgfuL4LE60hJAqn7LltQUg
Presented in #Pragma Conference in Verona in 2017
https://www.youtube.com/watch?v=yiE1R0qd1Gg
Eleni Papanikolopoulou
October 12, 2017
Transcript
121 slides
-
Eleni Papanikolopoulou iOS Developer @Workable @elenipapanikolo ERROR HANDLING MADE EASY
Kostas Kremizas iOS Developer @Workable @kremizask -
Error = “a value used to report that an error
condition occurred and normal functionality was skipped” Definitions – Matt Gallagher, Cocoa With Love -
Error Handling = “code that looks for errors and performs
different actions based on the presence of those errors” Error = “a value used to report that an error condition occurred and normal functionality was skipped” Definitions – Matt Gallagher, Cocoa With Love -
– Swift Apprentice, Chapter 22 (Error Handling) “Error handling is
the art of failing gracefully” -
Why bother? More and more critical actions in apps Apps
often rely on unreliable sources -
Why bother? More and more critical actions in apps Apps
often rely on unreliable sources Bad error handling => users don't TRUST our app -
Steps 1. List possible errors 2. Handle each error For
each method that can error out: -
Technical • Network • Disk • Server unavailability 1. List
possible errors -
Technical • Network • Disk • Server unavailability 1. List
possible errors • Authorization • Validation • Stale or bad data Business -
2. Handle each error Present a relevant error message Additional
actions per case -
2. Handle each error Present a relevant error message Additional
actions per case Log & report the error (always) -
– Jan L. A. van de Snepscheut “In theory there
is no difference between theory and practice. In practice there is.” -
@IBAction func loginTapped(_ sender: Any) { api.login(email, password) { (response,
error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showAlert(message: "Incorrect email or password") -
@IBAction func loginTapped(_ sender: Any) { api.login(email, password) { (response,
error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showAlert(message: "Incorrect email or password") -
@IBAction func loginTapped(_ sender: Any) { api.login(email, password) { (response,
error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showAlert(message: "Incorrect email or password") case let error as NSError where error.domain == NSURLErrorDomain && error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") -
@IBAction func loginTapped(_ sender: Any) { api.login(email, password) { (response,
error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showAlert(message: "Incorrect email or password") case let error as NSError where error.domain == NSURLErrorDomain && error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") -
@IBAction func loginTapped(_ sender: Any) { api.login(email, password) { (response,
error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showAlert(message: "Incorrect email or password") case let error as NSError where error.domain == NSURLErrorDomain && error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") case let error as SignInError where error == .invalidEmail: showInvalidEmail() -
@IBAction func loginTapped(_ sender: Any) { api.login(email, password) { (response,
error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showAlert(message: "Incorrect email or password") case let error as NSError where error.domain == NSURLErrorDomain && error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") case let error as SignInError where error == .invalidEmail: showInvalidEmail() -
@IBAction func loginTapped(_ sender: Any) { api.login(email, password) { (response,
error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showAlert(message: "Incorrect email or password") case let error as NSError where error.domain == NSURLErrorDomain && error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") case let error as SignInError where error == .invalidEmail: showInvalidEmail() default: showError(message: "Sorry, there was a problem. Please try again") -
@IBAction func loginTapped(_ sender: Any) { api.login(email, password) { (response,
error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showAlert(message: "Incorrect email or password") case let error as NSError where error.domain == NSURLErrorDomain && error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") case let error as SignInError where error == .invalidEmail: showInvalidEmail() default: showError(message: "Sorry, there was a problem. Please try again") Logger.log(error) -
Add error handling to a Compose email screen [email protected] Issue
Dear Natalie, I would like to ask you about a recent purchase of your product. I've having issues with... Thanks, Bob -
Add error handling to a Compose email screen [email protected] Issue
Dear Natalie, I would like to ask you about a recent purchase of your product. I've having issues with... Thanks, Bob -
@IBAction func sendTapped(_ sender: Any) { api.send(emailData) { (response, error)
in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showSignInScreen() -
@IBAction func sendTapped(_ sender: Any) { api.send(emailData) { (response, error)
in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showSignInScreen() -
case let error as NSError where error.domain == NSURLErrorDomain &&
error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") @IBAction func sendTapped(_ sender: Any) { api.send(emailData) { (response, error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showSignInScreen() -
case let error as NSError where error.domain == NSURLErrorDomain &&
error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") @IBAction func sendTapped(_ sender: Any) { api.send(emailData) { (response, error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showSignInScreen() -
case let error as NSError where error.domain == NSURLErrorDomain &&
error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") @IBAction func sendTapped(_ sender: Any) { api.send(emailData) { (response, error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showSignInScreen() default: showError(message: "Sorry, there was a problem. Please try again") -
case let error as NSError where error.domain == NSURLErrorDomain &&
error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") @IBAction func sendTapped(_ sender: Any) { api.send(emailData) { (response, error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showSignInScreen() default: showError(message: "Sorry, there was a problem. Please try again") -
case let error as NSError where error.domain == NSURLErrorDomain &&
error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") @IBAction func sendTapped(_ sender: Any) { api.send(emailData) { (response, error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showSignInScreen() default: showError(message: "Sorry, there was a problem. Please try again") showMessage("Don't worry, a draft has been saved.") -
case let error as NSError where error.domain == NSURLErrorDomain &&
error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") @IBAction func sendTapped(_ sender: Any) { api.send(emailData) { (response, error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showSignInScreen() default: showError(message: "Sorry, there was a problem. Please try again") showMessage("Don't worry, a draft has been saved.") Logger.log(error) -
case let error as NSError where error.domain == NSURLErrorDomain &&
error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") @IBAction func sendTapped(_ sender: Any) { api.send(emailData) { (response, error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showSignInScreen() default: showError(message: "Sorry, there was a problem. Please try again") showMessage("Don't worry, a draft has been saved.") Logger.log(error) common -
case let error as NSError where error.domain == NSURLErrorDomain &&
error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") @IBAction func sendTapped(_ sender: Any) { api.send(emailData) { (response, error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showSignInScreen() default: showError(message: "Sorry, there was a problem. Please try again") showMessage("Don't worry, a draft has been saved.") Logger.log(error) common with a twist -
case let error as NSError where error.domain == NSURLErrorDomain &&
error.code == NSURLErrorNotConnectedToInternet: showWarning(“You appear to be offline. Please check your connection.") @IBAction func sendTapped(_ sender: Any) { api.send(emailData) { (response, error) in } } if let error = error { } switch error { } case let error as HttpError where error.status == 401: showSignInScreen() default: showError(message: "Sorry, there was a problem. Please try again") showMessage("Don't worry, a draft has been saved.") Logger.log(error) common with a twist NEW -
We end up with… Boilerplate Repetition No standard way of
handling Cognitive overhead -
Bad practices Skip cases and use generic error messages Don’t
abstract and leave duplicate code everywhere -
Bad practices Skip cases and use generic error messages Don’t
abstract and leave duplicate code everywhere Handle errors at the network layer ! -
“Can I have a pizza with dough, buffalo mozzarella, tomato,
tomato sauce, basil, olives and ham please?” - noone ever -
Instead we say… “The usual…” or “Capricciosa pizza with extra
olives without the mushrooms” -
A set of default actions for common errors What we
actually want -
A set of default actions for common errors An easy
way to customise these defaults Add new cases Override existing ones Add actions for unknown errors Add actions for all errors (logging) What we actually want -
ErrorHandler A library that provides a declarative fluent API for
flexible error handling -
ErrorHandler public func on(matches: Error -> Bool, do action: @escaping
ErrorAction) -> ErrorHandler Basic API -
ErrorHandler public func on(matches: Error -> Bool, do action: @escaping
ErrorAction) -> ErrorHandler public func always(do action: @escaping ErrorAction) -> ErrorHandler Basic API -
ErrorHandler public func on(matches: Error -> Bool, do action: @escaping
ErrorAction) -> ErrorHandler public func always(do action: @escaping ErrorAction) -> ErrorHandler public func onNoMatch(do action: @escaping ErrorAction) -> ErrorHandler Basic API -
ErrorHandler public func on(matches: Error -> Bool, do action: @escaping
ErrorAction) -> ErrorHandler public func handle(_ error: Error) public func always(do action: @escaping ErrorAction) -> ErrorHandler public func onNoMatch(do action: @escaping ErrorAction) -> ErrorHandler Basic API -
public func on(matches: Error -> Bool, do action: @escaping ErrorAction)
-> ErrorHandler public func handle(_ error: Error) public func always(do action: @escaping ErrorAction) -> ErrorHandler public func on(error: Error & Equatable, do action: @escaping ErrorAction) -> ErrorHandler Basic API public func onNoMatch(do action: @escaping ErrorAction) -> ErrorHandler -
Our strategy Setup a default ErrorHandler once Customize the default
ErrorHandler when needed based on the context -
In many cases all we will need is… if let
error = error { return } -
In many cases all we will need is… ErrorHandler.defaultHandler.handle(error) if
let error = error { return } -
extension ErrorHandler { class var defaultHandler: ErrorHandler { } }
return ErrorHandler() -
extension ErrorHandler { class var defaultHandler: ErrorHandler { } }
return ErrorHandler() .on(NSError(domain:NSURLErrorDomain, code: NSURLErrorNotConnectedToInternet), do: { (error) -> MatchingPolicy in showWarning(“You appear to be offline. Please check your connection.") return .continueMatching }) -
.on(httpStatus: 401, do: { (error) -> MatchingPolicy in showSignInScreen() return
.continueMatching }) extension ErrorHandler { class var defaultHandler: ErrorHandler { } } return ErrorHandler() .on(NSError(domain:NSURLErrorDomain, code: NSURLErrorNotConnectedToInternet), do: { (error) -> MatchingPolicy in showWarning(“You appear to be offline. Please check your connection.") return .continueMatching }) -
.always(do: { (error) -> MatchingPolicy in Logger.log(error) return .continueMatching })
.on(httpStatus: 401, do: { (error) -> MatchingPolicy in showSignInScreen() return .continueMatching }) extension ErrorHandler { class var defaultHandler: ErrorHandler { } } return ErrorHandler() .on(NSError(domain:NSURLErrorDomain, code: NSURLErrorNotConnectedToInternet), do: { (error) -> MatchingPolicy in showWarning(“You appear to be offline. Please check your connection.") return .continueMatching }) -
.always(do: { (error) -> MatchingPolicy in Logger.log(error) return .continueMatching })
.on(httpStatus: 401, do: { (error) -> MatchingPolicy in showSignInScreen() return .continueMatching }) extension ErrorHandler { class var defaultHandler: ErrorHandler { } } return ErrorHandler() .on(NSError(domain:NSURLErrorDomain, code: NSURLErrorNotConnectedToInternet), do: { (error) -> MatchingPolicy in showWarning(“You appear to be offline. Please check your connection.") return .continueMatching }) .onNoMatch(do: { (error) -> MatchingPolicy in showError(message: "Sorry, there was a problem. Please try again") return .continueMatching }) -
@IBAction func loginTapped(_ sender: Any) { api.login(email, password) { (response,
error) in if let error = error { } } } -
@IBAction func loginTapped(_ sender: Any) { api.login(email, password) { (response,
error) in if let error = error { } } } ErrorHandler.defaultHandler .on(httpStatus: 401, do: { (_) -> MatchingPolicy in showAlert(message: "Incorrect email or password") return .stopMatching }) .on(SignInError.invalidEmail, do: { [weak self] (_) -> MatchingPolicy in self?.showInvalidEmail() return .stopMatching }) .handle(error) -
@IBAction func sendTapped(_ sender: Any) { api.send(emailData) { (response, error)
in if let error = error { } } } -
ErrorHandler.defaultHandler .always(do: { (error) -> MatchingPolicy in self.showMessage("Don't worry, a
draft has been saved.") return .continueMatching }) .handle(error) @IBAction func sendTapped(_ sender: Any) { api.send(emailData) { (response, error) in if let error = error { } } } -
Avoid the boilerplate Repetition No standard way of handling Cognitive
overhead We get to.. -
Avoid the repetition Avoid the boilerplate No standard way of
handling Cognitive overhead We get to.. -
Avoid the repetition Have a standard way of handling Avoid
the boilerplate Cognitive overhead We get to.. -
Eliminate cognitive overhead Avoid the repetition Have a standard way
of handling Avoid the boilerplate We get to.. -
public protocol ErrorMatcher { func matches(_ error: Error) -> Bool
} ErrorMatcher handler .on(matcher) { (error) -> MatchingPolicy in showAlert(message: "It's a match!") return .stopMatching }.handle(error) -
ErrorMatcher ErrorMatcher && || let notConnectedMatcher = NSErrorMatcher(domain: NSURLErrorDomain, code:
NSURLErrorNotConnectedToInternet) let connectionLostMatcher = NSErrorMatcher(domain: NSURLErrorDomain, code: NSURLErrorNetworkConnectionLost) -
ErrorMatcher ErrorMatcher && || let notConnectedMatcher = NSErrorMatcher(domain: NSURLErrorDomain, code:
NSURLErrorNotConnectedToInternet) let connectionLostMatcher = NSErrorMatcher(domain: NSURLErrorDomain, code: NSURLErrorNetworkConnectionLost) let offlineMatcher = notConnectedMatcher || connectionLostMatcher -
// At the setup point of our default handler return
ErrorHandler() // other setup code .tag(notConnectedMatcher, with: "offline") .tag(connectionLostMatcher, with: "offline") // At the point where we handle an error e.g. any controller ErrorHandler() .on(tag: "offline", do: { (error) -> MatchingPolicy in showError("You appear to be offline. Please check you connection.) return .continueMatching }).handle(error) Tag -
Additional features Error Matchers Tags Extension for http status error
handling (Alamofire out of the box) -
Conclusions Error handling is an integral part of a good
UX It can get cumbersome if you want to do it right You can minimize the friction with the right abstractions -
Questions? Even if there are many error cases, it’s nothing
you can’t .handle() https://github.com/Workable/swift-error-handler.git https://github.com/Workable/java-error-handler.git