1 min read

Preventing Sentry from Reporting SwiftUI Preview Errors

Preventing Sentry from Reporting SwiftUI Preview Errors
Photo by Luke Chesser / Unsplash

One challenge with using Sentry in SwiftUI is its interaction with Previews. Since Previews essentially run the app similarly to the Simulator, any crashes or issues that occur during the Preview process are reported to Sentry.

However, these errors are usually minor and quickly resolved in Xcode during development. As a result, the Sentry dashboard can become cluttered with unnecessary and invalid reports.

We can fix it with some options when we setup Sentry workflow in our project:

SentrySDK.start { options in
    ...
    
    options.beforeSend = { event in
        // Check if the event is from a simulator
        if event.context?["device"]?["simulator"] as? Bool == true {
            // Return nil to drop the event
            return nil
        }
        // Otherwise, send the event
        return event
    }
}