Muta will automatically handle errors and prevent the flow from displaying if there are any issues. You can listen for these errors to show appropriate messages to your users.
Example Usage
import MutaSDK
import SwiftUI
struct OnboardingView: View {
@State private var errorMessage: String?
@State private var showError = false
var body: some View {
VStack {
// Your content
}
.alert("Error", isPresented: $showError) {
Button("OK", role: .cancel) { }
} message: {
Text(errorMessage ?? "An unknown error occurred")
}
.onAppear {
setupErrorHandling()
}
}
private func setupErrorHandling() {
let subscription = Muta.shared.on { event in
if case .error(let error) = event {
switch error {
case .network(let message, _):
errorMessage = "Please check your internet connection and try again."
showError = true
case .placement(let message, _, _, _):
errorMessage = "Unable to load the onboarding flow. Please try again later."
showError = true
}
}
}
// Show the placement
Muta.shared.displayPlacement(
placementId: "your-placement-id",
backgroundColor: .white
)
}
}
Error Types
1. Network Errors
- Emitted when there’s no internet connection
- The flow will not display
- You should show an appropriate “No internet connection” message
2. Placement Errors
- Emitted when trying to display a placement that has been deleted
- May indicate an incorrect placement ID or API key
- The flow will not display
- You should show an appropriate error message to the user
Error Event Structure
Network Error
case network(message: String, timestamp: Int)
// message: Error message
// timestamp: Unix timestamp in ms
Placement Error
case placement(message: String, code: String, timestamp: Int, placementId: String)
// message: Error message
// code: Error code
// timestamp: Unix timestamp in ms
// placementId: ID of the placement that failed
Best Practices
-
User-Friendly Messages: Display error messages that are clear and actionable for users.
-
Error Recovery: Implement retry mechanisms for network errors.
-
Error Logging: Log errors for debugging and monitoring purposes.
-
Fallback Content: Provide fallback content or alternative flows when errors occur.
Example Implementation
import MutaSDK
import SwiftUI
struct OnboardingView: View {
@State private var errorMessage: String?
@State private var showError = false
@State private var showRetry = false
var body: some View {
VStack {
// Your content
}
.alert("Error", isPresented: $showError) {
if showRetry {
Button("Retry") {
retryPlacement()
}
}
Button("OK", role: .cancel) { }
} message: {
Text(errorMessage ?? "An unknown error occurred")
}
.onAppear {
setupErrorHandling()
}
}
private func setupErrorHandling() {
let subscription = Muta.shared.on { event in
if case .error(let error) = event {
switch error {
case .network(let message, _):
errorMessage = "Please check your internet connection and try again."
showError = true
showRetry = true
case .placement(let message, _, _, _):
errorMessage = "Unable to load the onboarding flow. Please try again later."
showError = true
showRetry = false
}
}
}
// Show the placement
Muta.shared.displayPlacement(
placementId: "your-placement-id",
backgroundColor: .white
)
}
private func retryPlacement() {
Muta.shared.displayPlacement(
placementId: "your-placement-id",
backgroundColor: .white
)
}
}
Error Codes
Network Errors
NETWORK_ERROR
: General network connectivity issues
TIMEOUT_ERROR
: Request timeout
OFFLINE_ERROR
: Device is offline
Placement Errors
INVALID_PLACEMENT_ID
: The provided placement ID is invalid or doesn’t exist
INVALID_API_KEY
: The API key is invalid or has been revoked
PLACEMENT_NOT_FOUND
: The placement could not be found
PLACEMENT_DISABLED
: The placement has been disabled
PLACEMENT_EXPIRED
: The placement has expired