A Library for handling errors in SwiftUI Interfaces.
Find a file
2024-04-12 18:44:35 +02:00
.github/workflows ci: Create documentation workflow 2024-04-12 18:44:35 +02:00
.swiftpm/xcode update xcshareddata 2024-04-12 18:39:34 +02:00
Sources docs: fix warnings 2024-04-12 02:53:12 +02:00
Tests/SwiftUIErrorTests chore: update SwiftUIError library with new features and improvements 2024-02-17 15:19:58 +01:00
.gitignore Initial Commit 2024-02-16 17:36:54 +01:00
Package.resolved chore: update package platforms and dependencies 2024-02-16 19:08:37 +01:00
Package.swift chore: add SwiftUIErrorExample target to the package 2024-02-16 19:59:22 +01:00
README.md chore: update SwiftUIError library with new features and improvements 2024-02-17 15:19:58 +01:00

A Library for handling errors in SwiftUI Interfaces.

Like NavigationLink(, value:) .navigationDestination(for:,) but for error handling

Usage

Basic Usage

Check The Docc Documentation for more information on how to throw and catch errors

WithErrorHandling { errors in
    errors.push(URLError(.cancelled))
}
.onCatch(of: URLError.self) { err in
    print("Oh Oh", err)
}

Error Throwing & Catching Views

SwiftUIError also provides convenient wrappers around basic SwiftUI Views & View Modifiers like Button, Alert & Task

ThrowingButton("Press Me") {
    throw URLError(.cancelled)
}
.alert("A URLError occured", for: URLError.self, message: { error in
    switch error {
        case .cancelled:
            Text("The Operation was cancelled")
            
        default:
            Text("Unknown Error")
    }
})

View Hierarchy

An error can only be caught once. Error Catching Views will only push unknown errors upstream. This makes it possible to have multiple handlers for one error type.

If an error get's pushed all the way upstream without being caught it will emit a runtime warning (when debugging)

You can also add an onCatch() modifier without specifying an error type to catch any Error:

VStack {
    ThrowingButton("Not Caught at all") {
        throw URLError(.cancelled)
    }
        
    VStack {
        ThrowingButton("Caught by 1") {
            throw URLError(.cancelled)
        }
        .onCatch(of: URLError.self) { _ in }    // 1
        
        ThrowingButton("Caught by 2") {
            throw URLError(.cancelled)
        }
        
        ThrowingButton("Caught by 3") { 
            throw MyError()
        }
    }
    .onCatch(of: URLError.self) { _ in }        // 2
    
    .onCatch { error in                         // 3
        print("Unhandled Error", error)
    }
}