Skip to content

A very tiny repackage for SwiftUI's alert(isPresent:error:actions:) modifier.

Notifications You must be signed in to change notification settings

AreroKetahi/ErrorHandler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Error Handler

A very tiny repackage of SwiftUI's alert(isPresent:error:actions:) modifier.

This library dose not have enough localization strings for all the languanges, feel free for fork & submit PR!

Setup

To use error handler in you SwiftUI app, there have serval steps to initialize it. Before we start, don't forget to import ErrorHandler.

1. Setup environment.

Go to <Your Project Name>App.swift, add @Environment(\.errorHandler) private var handler inside the brace of your app.

@main
struct SampleApp: App {
    @Environment(\.errorHandler) private var handler
  
    // body: some Scene { ...
}

2. Setup alert

Then, add errorAlert(handler:) modifier to your ContentView.

@main
struct SampleApp: App {
    @Environment(\.errorHandler) private var handler
  
    var body: some Scene {
        WindowGroup {
            ContentView()
                .errorAlert(handler: handler)
        }
    }
}

3. Use it!

Once you setup error handler, you can use it anywhere. Here is a example

// You have you get handler from environment first.
//
// use 
// @Environment(\.errorHandler) private var handler
// to get it

withErrorHandler(handler) {
    // do something may occur error
}

Dive deeper

This library provide 2 major ways to handle errors.

  1. The most easy one, directly handle the error

    withErrorHandler(handler) {
        // actions may occur error
    }
  2. You can also execute custom action after user click "confirm" button.

withErrorHandler(handler) {
    // actions may occur error
} handlerAction: {
    // actions that will execute after user click "confirm" button
}

Library also provide asynchronous version functions to support concurrency, both of the are marked by @MainActor. Those function are support the throw-actions which need to run in asynchronous environment.

await withErrorHandler(handler) {
    // actions may occur error in asynchronous environment.
}

await withErrorHandler(handler) {
    // actions may occur error in asynchronous environment.
} handlerAction: {
    // actions that will execute after user click "confirm" button
    // handler action DOSE NOT SUPPORT concurrency
}

About

A very tiny repackage for SwiftUI's alert(isPresent:error:actions:) modifier.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages