Created
May 20, 2021 19:56
-
-
Save Nillerr/1a1669da5aef0463877e3f06b51a3c3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
import Foundation | |
import shared | |
extension KotlinThrowable: Error { | |
} | |
struct PublisherFailures { | |
/** | |
The action to invoke when a failure is dropped as the result of a `Publisher` returned by | |
`Publisher.completeOnFailure()`. | |
*/ | |
static var willCompleteOnFailure: (Error, Callsite) -> Void = { error, callsite in | |
if error.isKotlinCancellation { | |
return | |
} | |
print("[ERROR] A publisher failed and was completed due to a call to `completeOnFailure()` \(callsite): \(error)") | |
} | |
} | |
struct Callsite: CustomStringConvertible { | |
let file: String | |
let fileID: String | |
let filePath: String | |
let line: Int | |
let column: Int | |
let function: String | |
let dsoHandle: UnsafeRawPointer | |
var description: String { | |
"in \(function) at \(filePath)#\(line):\(column)" | |
} | |
} | |
extension Publisher { | |
/** | |
Ignores errors in the upstream publisher, emitting an empty sequence instead, and otherwise republishes all received input. | |
You can hook into these failures by assigning a function to `PublisherHooks.willCompleteOnFailure`. | |
*/ | |
func completeOnFailure(file: String = #file, fileID: String = #fileID, filePath: String = #filePath, line: Int = #line, column: Int = #column, function: String = #function, dsoHandle: UnsafeRawPointer = #dsohandle) -> Publishers.Catch<Self, Empty<Output, Never>> { | |
return `catch` { error in | |
let callsite = Callsite(file: file, fileID: fileID, filePath: filePath, line: line, column: column, function: function, dsoHandle: dsoHandle) | |
PublisherFailures.willCompleteOnFailure(error, callsite) | |
return Empty(completeImmediately: true) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment