Skip to content

Commit

Permalink
Report to unified logging when a promise violation is detected
Browse files Browse the repository at this point in the history
  • Loading branch information
EricRabil committed Aug 3, 2022
1 parent e7c5c1d commit cbd70ea
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/Pwomise/Pwomise.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import Swog

public protocol PromiseConvertible {
associatedtype Output
Expand All @@ -16,6 +17,7 @@ public protocol PromiseAbuseDelegate {
func promise<T>(_ promise: Promise<T>, doubleResolvedWithResult result: PendingPromise<T, Error>, resolutionStackTrace: [String], abusingStackTrace: [String])
}

private let abuseLog = Logger(category: "Abuse", subsystem: "com.ericrabil.pwomise")
public var SharedPromiseAbuseDelegate: PromiseAbuseDelegate?

typealias dispatch_get_current_queue_t = @convention(c) () -> Unmanaged<DispatchQueue>
Expand Down Expand Up @@ -100,6 +102,8 @@ public class Promise<Output>: CustomDebugStringConvertible {
willSet {
guard result == .pending, newValue != .pending else {
/// Result can only be set once – its a promise of a result, not a publisher
let resolutionStackTrace = resolutionStackTrace, callStackSymbols = Thread.callStackSymbols
abuseLog.fault("Double-completion of \(String(describing: type(of: self))) \n- original stack trace: \n\(resolutionStackTrace.joined(separator: "\n"))\n- violating stack trace: \(callStackSymbols)")
if let abuseDelegate = SharedPromiseAbuseDelegate {
abuseDelegate.promise(self, doubleResolvedWithResult: newValue, resolutionStackTrace: resolutionStackTrace, abusingStackTrace: Thread.callStackSymbols)
} else {
Expand Down

0 comments on commit cbd70ea

Please sign in to comment.