Git Product home page Git Product logo

errorhandling's Introduction

ErrorHandling

An easy way to thoroughly handle errors in SwiftUI, with support for retry/recovery and sign out.

By defaults errors are presented with alerts, and this can be customized by setting .environment(\.errorHandler, newErrorHandler).

Errors can also be categorized into recoverable, non-recoverable and requiring signout. The alert error handler will provide a button to attempt to recover recoverable errors. Errors requiring signout will automatically invoke the \.signOutHandler environment value. This should be set in the root view of your app.

Installation

CocoaPods:

pod 'ErrorHandling', git: '[email protected]:RobotsAndPencils/ErrorHandling.git', tag: 'v0.1.0'

Swift Package Manager:

.package(url: "[email protected]:RobotsAndPencils/ErrorHandling.git", .upToNextMinor(from: "0.1.0")),

Usage

An example project is provided.

Use the emittingError(_:recoveryHandler:) modifier from a view that should present an error:

@State private var error: Error?

var body: some View {
    // ...
    .emittingError($error, recoveryHandler: { recoveryOption in
        switch recoveryOption {
        case .resubmit:
            submit()
        case .resave:
            save()
        default:
            break
        }
    })
}

Define RecoveryOptions that you'd like users to be able to perform.

public extension RecoveryOption {
    static let resubmit = RecoveryOption(id: "com.robotsandpencils.Tailboard.Resubmit", description: "Resubmit")
}

Add a CategorizedError conformance to your Error types. This is recommended especially for authentication errors so your users will be automatically signed out when appropriate. You can also return an appropriate RecoveryOption here.

extension ResponseCodeError: CategorizedError {
    public var category: ErrorCategory {
        switch self {
        case let .invalidResponseCode(possibleResponse, _) where possibleResponse?.statusCode == 401:
            return .requiresSignout
        default:
            return .nonRecoverable
        }
    }
}

extension AppStateError: CategorizedError {
    var category: ErrorCategory {
        switch self {
        case let .submitDraft(error):
            if error.resolveCategory() == .requiresSignout {
                return .requiresSignout
            } else {
                return .recoverable(recoveryOption: .resubmit)
            }
        default:
            return .nonRecoverable
        }
    }
}

Set a signOutHandler environment value on your root views. This can be any () -> Void function.

window.rootViewController = UIHostingController(
    rootView: RootView()
        .environment(\.signOutHandler, appState.signOut)
)

Reference

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.