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!
To use error handler in you SwiftUI app, there have serval steps to initialize it. Before we start, don't forget to import ErrorHandler
.
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 { ...
}
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)
}
}
}
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
}
This library provide 2 major ways to handle errors.
-
The most easy one, directly handle the error
withErrorHandler(handler) { // actions may occur error }
-
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
}