Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-fatal error reporter #91

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/UBFoundation/Logging/UBNonFatalErrorReporter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// UBNonFatalErrorReporter.swift
//
//
// Created by Matthias Felix on 11.06.2024.
//

import Foundation

public enum UBNonFatalErrorReporter {
public static var handler: ((Error) -> Void)?

static func report(_ error: Error) {
handler?(error)
}
}
4 changes: 3 additions & 1 deletion Sources/UBFoundation/Networking/UBURLDataTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,9 @@ public final class UBURLDataTask: UBURLSessionTask, CustomStringConvertible, Cus
(.cancelled, .waitingExecution): // Restart task
break
default:
fatalError("Invalid state transition from \(_state) -> \(newValue)")
let errorMessage = "Invalid state transition from \(_state) -> \(newValue)"
assertionFailure(errorMessage)
UBNonFatalErrorReporter.report(NSError(domain: "UBURLDataTask", code: 0, userInfo: [NSLocalizedDescriptionKey: errorMessage]))
Comment on lines +433 to +435
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve error handling by encapsulating error creation.

let errorMessage = "Invalid state transition from \(_state) -> \(newValue)"
assertionFailure(errorMessage)
UBNonFatalErrorReporter.report(createError(withMessage: errorMessage))

// Suggested helper function
private func createError(withMessage message: String) -> NSError {
    return NSError(domain: "UBURLDataTask", code: 0, userInfo: [NSLocalizedDescriptionKey: message])
}
Tools
SwiftLint

[Warning] 435-435: Line should be 120 characters or less; currently it has 146 characters (line_length)

}
}
didSet {
Expand Down
Loading