Skip to content

Instantly share code, notes, and snippets.

@achernoprudov
Created August 4, 2020 05:12
Show Gist options
  • Save achernoprudov/a9a268f3344bf95dfd387db3ad1109b0 to your computer and use it in GitHub Desktop.
Save achernoprudov/a9a268f3344bf95dfd387db3ad1109b0 to your computer and use it in GitHub Desktop.
Error Reporter facade for non fatals errors
import Foundation
public class ErrorReporter {
// MARK: - Aliases
public typealias ReportFunction = (
_ error: Error,
_ file: StaticString,
_ line: UInt
) -> Void
// MARK: - Instance variables
private let reportFunction: ReportFunction
// MARK: - Public
public init(from reportFunction: @escaping ReportFunction) {
self.reportFunction = reportFunction
}
func report(_ error: Error, file: StaticString, line: UInt) {
reportFunction(error, file, line)
}
}
public class ErrorReporterFacade {
// MARK: - Static
public static let `default` = ErrorReporterFacade()
// MARK: - Instance variables
private var reporters: [ErrorReporter] = []
// MARK: - Public
public func report(
_ error: Error,
file: StaticString = #file,
line: UInt = #line
) {
for reporter in reporters {
reporter.report(error, file: file, line: line)
}
}
public func register(with reportFunction: @escaping ErrorReporter.ReportFunction) {
let reporter = ErrorReporter(from: reportFunction)
register(reporter: reporter)
}
public func register(reporter: ErrorReporter) {
reporters.append(reporter)
}
// MARK: - Private
private init() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment