|
| 1 | +import Foundation |
| 2 | +import ObjectiveC |
| 3 | +import CrashKiOSBugsnagObjC |
| 4 | +import Bugsnag |
| 5 | + |
| 6 | +/// Feature flag used to mark the Kotlin termination crash. |
| 7 | +/// Public so custom `CrashKiOSBugsnagSink` implementations can honor the same contract. |
| 8 | +public let kotlinCrashedFeatureFlag = "crashkios.kotlin_crashed" |
| 9 | + |
| 10 | +/// Prepares `config` for Kotlin crash handling: installs the OnSendError filter that |
| 11 | +/// suppresses the duplicate termination crash following a recorded Kotlin fatal, and |
| 12 | +/// works around Bugsnag 6.26.2+ refusing to persist synthetic unhandled events. |
| 13 | +/// |
| 14 | +/// Custom sink implementations MUST call this before `Bugsnag.start(with: config)` — |
| 15 | +/// otherwise every fatal Kotlin exception is reported twice. |
| 16 | +public func configureBugsnagForKotlin(_ config: BugsnagConfiguration) { |
| 17 | + overrideOriginalUnhandledValue() |
| 18 | + config.addOnSendError { event in |
| 19 | + // Drop Bugsnag's own report of the abort that terminates the app after a |
| 20 | + // Kotlin fatal was already recorded (flagged via markFatalCrashRecorded). |
| 21 | + !event.unhandled || !event.featureFlags.contains { $0.name == kotlinCrashedFeatureFlag } |
| 22 | + } |
| 23 | + config.clearFeatureFlag(name: kotlinCrashedFeatureFlag) |
| 24 | +} |
| 25 | + |
| 26 | +/// Reference `CrashKiOSBugsnagSink` implementation. |
| 27 | +/// |
| 28 | +/// Start Bugsnag through this class so the configuration is prepared for Kotlin |
| 29 | +/// crash handling, then register the returned sink with the Kotlin side: |
| 30 | +/// ```swift |
| 31 | +/// let sink = BugsnagSink.start(BugsnagConfiguration.loadConfig()) |
| 32 | +/// BugsnagKt.registerBugsnagSink(sink: sink) |
| 33 | +/// ``` |
| 34 | +/// (Add `export("co.touchlab.crashkios:bugsnag")` to the Kotlin framework for clean names.) |
| 35 | +public final class BugsnagSink: NSObject, CrashKiOSBugsnagSink { |
| 36 | + |
| 37 | + /// Configures `config` via `configureBugsnagForKotlin(_:)`, starts Bugsnag, |
| 38 | + /// and returns the sink. |
| 39 | + public static func start(_ config: BugsnagConfiguration) -> BugsnagSink { |
| 40 | + configureBugsnagForKotlin(config) |
| 41 | + Bugsnag.start(with: config) |
| 42 | + return BugsnagSink() |
| 43 | + } |
| 44 | + |
| 45 | + public func leaveBreadcrumb(_ message: String) { |
| 46 | + Bugsnag.leaveBreadcrumb(withMessage: message) |
| 47 | + } |
| 48 | + |
| 49 | + public func notify(with exceptions: [NSException], handled: Bool) { |
| 50 | + guard let exception = exceptions.first else { return } |
| 51 | + // Notify persists unhandled events, so the caller can safely terminate afterwards. |
| 52 | + // https://github.com/bugsnag/bugsnag-cocoa/blob/6bcd46f5f8dc06ac26537875d501f02b27d219a9/Bugsnag/Client/BugsnagClient.m#L744 |
| 53 | + Bugsnag.notify(exception) { event in |
| 54 | + if handled { |
| 55 | + event.severity = .warning |
| 56 | + } else { |
| 57 | + event.unhandled = true |
| 58 | + event.severity = .error |
| 59 | + } |
| 60 | + event.errors += exceptions.dropFirst().map(BugsnagError.init) |
| 61 | + return true |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public func addMetadata(_ value: Any, key: String, section: String) { |
| 66 | + Bugsnag.addMetadata(value, key: key, section: section) |
| 67 | + } |
| 68 | + |
| 69 | + public func markFatalCrashRecorded() { |
| 70 | + // Called only by the Kotlin unhandled-exception hook, right before termination — |
| 71 | + // never on direct sendFatalException() calls, which would poison the session and |
| 72 | + // make the OnSendError filter discard later genuine crashes. |
| 73 | + Bugsnag.addFeatureFlag(name: kotlinCrashedFeatureFlag) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// In Bugsnag 6.26.2+ the `originalUnhandledValue` property prevents our synthetic |
| 78 | +/// unhandled exceptions from being stored to disk; alias it to `unhandled`. |
| 79 | +/// https://github.com/bugsnag/bugsnag-cocoa/pull/1549 |
| 80 | +private func overrideOriginalUnhandledValue() { |
| 81 | + guard let handledStateClass = NSClassFromString("BugsnagHandledState"), |
| 82 | + let originalMethod = class_getInstanceMethod(handledStateClass, NSSelectorFromString("originalUnhandledValue")), |
| 83 | + let method = class_getInstanceMethod(handledStateClass, NSSelectorFromString("unhandled")) |
| 84 | + else { |
| 85 | + // Loud, not silent: if Bugsnag renames these internals, fatal Kotlin crashes |
| 86 | + // would silently stop reaching the dashboard (>=6.26.2 refuses to persist them). |
| 87 | + assertionFailure("CrashKiOS: BugsnagHandledState internals changed — Kotlin fatal crashes may not be persisted. Update CrashKiOSBugsnag.") |
| 88 | + return |
| 89 | + } |
| 90 | + method_setImplementation(originalMethod, method_getImplementation(method)) |
| 91 | +} |
| 92 | + |
| 93 | +private extension BugsnagError { |
| 94 | + /// Creates a BugsnagError from a (Kotlin-synthesized) NSException. |
| 95 | + convenience init(_ exception: NSException) { |
| 96 | + self.init() |
| 97 | + errorClass = exception.name.rawValue |
| 98 | + errorMessage = exception.reason |
| 99 | + stacktrace = BugsnagStackframe.stackframes(withCallStackReturnAddresses: exception.callStackReturnAddresses) |
| 100 | + type = .cocoa |
| 101 | + } |
| 102 | +} |
0 commit comments