Skip to content

Instantly share code, notes, and snippets.

@nebsta
Created September 27, 2024 09:27
Show Gist options
  • Save nebsta/dc3a556018e82cc295f6e401e6fa91dd to your computer and use it in GitHub Desktop.
Save nebsta/dc3a556018e82cc295f6e401e6fa91dd to your computer and use it in GitHub Desktop.
Bootstrapper.swift
// Copyright © 2022 Trustpilot. All rights reserved.
import Foundation
import Sentry
import TrustpilotMacros
/// Main implementation for handling initial loading of the application
@TPDependencies<
SentryUtilityProtocol,
RootStateMachineProtocol,
LocalNotificationServiceManagerProtocol
>
actor Bootstrapper: BoostrapperProtocol {
private let items: [BootstrapItemProtocol]
/// - Parameter items: Bootstrap items that should be triggered by the bootstrapper
init(
items: [BootstrapItemProtocol],
dependencies: Dependencies = .init()
) {
self.dependencies = dependencies
self.items = items
}
func loadAll() async throws {
let transaction = dependencies.sentry.startTransaction(name: "Bootstrap", operation: "Start")
for item in items {
let itemName = String(describing: type(of: item))
TLog.bootstrap.info("Loading item: \(itemName)")
dependencies.sentry.addBreadcrumb(BootstrapItemBreadcrumb.started(name: itemName))
var span: SentrySpanProtocol?
do {
span = transaction?.startChild(operation: itemName)
try await item.load()
try Task.checkCancellation()
dependencies.sentry.addBreadcrumb(BootstrapItemBreadcrumb.finished(name: itemName))
span?.finish()
} catch {
TLog.bootstrap.error("Error encountered on item: \(itemName), error: \(error)")
span?.finish(status: .internalError)
transaction?.finish()
dependencies.sentry.addBreadcrumb(BootstrapItemBreadcrumb.failed(name: itemName))
/// Some bootstrap items may not need to raise errors up to Sentry, so we log them here instead.
guard item.shouldRaiseFailureToSentry else {
TLog.bootstrap.error("Error encountered on item: \(itemName), error: \(error)")
return
}
throw BootstrapError.itemFailed(itemName: String(itemName), error: error)
}
}
transaction?.finish()
dependencies.sentry.stop()
dependencies.sentry.start()
await dependencies.rootStateMachine.observeDropLock()
try Task.checkCancellation()
dependencies.localNotificationServiceManager.startServices()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment