Skip to content

Commit

Permalink
Remove error merge
Browse files Browse the repository at this point in the history
  • Loading branch information
KaQuMiQ authored Jun 16, 2023
1 parent 498e685 commit 924b841
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 106 deletions.
91 changes: 3 additions & 88 deletions Sources/MQ/Errors/TheError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ public protocol TheError: Error, CustomStringConvertible, CustomDebugStringConve
/// with this error instance. It can be used
/// to quickly identify error domains or
/// group errors by any other meaning.
/// Default implementation does not support mutations,
/// if you need to mutate it please define stored
/// property in your error instance.
/// Default group for all errors is ``TheErrorGroup.default``.
var group: TheErrorGroup { get set }
var group: TheErrorGroup { get }

/// Source code metadata context for this error.
/// It is used to derive default implementations
Expand All @@ -34,20 +31,6 @@ public protocol TheError: Error, CustomStringConvertible, CustomDebugStringConve
/// It is used to derive default implementations
/// of ``Hashable`` and ``Equatable`` protocols.
var displayableString: DisplayableString { get }
/// ``merge`` adds contents of provided error to this error.
///
/// When merging, composite should take all informations
/// that it can handle and update self with new data.
/// It is intended to change or add error details
/// when i.e. catching other error while trying to keep
/// diagnostics data in place.
///
/// - Parameter other: Other error to be merged into this one.
mutating func merge(
with other: TheError,
file: StaticString,
line: UInt
)
/// Function checking equality of errors.
///
/// This function can be used to check if two errors
Expand Down Expand Up @@ -143,61 +126,6 @@ extension TheError /* DisplayableWithString */ {
}
}

extension TheError {

/// ``merge`` adds contents of provided error to this error.
///
/// When merging, composite should take all informations
/// that it can handle and update self with new data.
/// It is intended to change or add error details
/// when i.e. catching other error while trying to keep
/// diagnostics data in place.
///
/// - Parameters:
/// - other: Other error to be merged into this one.
/// - file: Source code file identifier.
/// Filled automatically based on compile time constants.
/// - line: Line in given source code file.
/// Filled automatically based on compile time constants.
public mutating func merge(
with other: TheError,
file: StaticString = #fileID,
line: UInt = #line
) {
self.group = .merging(self.group, other.group)
self.context = .merging(self.context, other.context)
}

/// ``merging`` adds contents of provided error to this error copy.
///
/// When merging, composite should take all informations
/// that it can handle and update self with new data.
/// It is intended to change or add error deatails
/// when i.e. catching other error while trying to keep
/// diagnostics data in place.
///
/// - Parameters:
/// - other: Other error to be merged into this error copy.
/// - file: Source code file identifier.
/// Filled automatically based on compile time constants.
/// - line: Line in given source code file.
/// Filled automatically based on compile time constants.
/// - Returns: Copy of this error with error merged.
public func merging(
with other: TheError,
file: StaticString = #fileID,
line: UInt = #line
) -> TheError {
var copy: Self = self
copy.merge(
with: other,
file: file,
line: line
)
return copy
}
}

// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
extension TheError {

Expand Down Expand Up @@ -231,21 +159,8 @@ extension TheError {
.init(Self.self)
}

public var group: TheErrorGroup {
@_transparent @_semantics("constant_evaluable") get {
.default
}
set {
Unimplemented
.error(
message: "Error group assignment is not implemented!"
)
.with(Self.self, for: "Error type")
.asRuntimeWarning(
message: "Cannot assign error group without stored property, please define one or avoid mutating it."
)
}
}
@_transparent @_semantics("constant_evaluable")
public var group: TheErrorGroup { .default }

/// Terminate process with this error as the cause.
///
Expand Down
10 changes: 8 additions & 2 deletions Sources/MQ/Errors/TheErrorGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ public struct TheErrorGroup {

extension TheErrorGroup {

internal static func merging(
/// Merge multiple error groups.
///
/// Note that ordering matters.
public static func merging(
_ head: TheErrorGroup,
_ mid: TheErrorGroup,
_ tail: TheErrorGroup...
) -> Self {
.merging([head, mid] + tail)
}

internal static func merging(
/// Merge multiple error groups.
///
/// Note that ordering matters.
public static func merging(
_ groups: Array<TheErrorGroup>
) -> Self {
var added: Set<TheErrorGroup.Identifier> = .init()
Expand Down
8 changes: 0 additions & 8 deletions Tests/MQTests/MultipleIssuesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ final class MultipleIssuesTests: XCTestCase {
XCTAssertTrue(multipleIssues.errors.contains(where: { $0 is Undefined }))
}

func test_merge_doesNotAddErrorToCollection() {
var multipleIssues: MultipleIssues = .error()

multipleIssues.merge(with: Undefined.error())

XCTAssertTrue(multipleIssues.errors.isEmpty)
}

func test_displayableString_usesProvidedExtractionFunction() {
let multipleIssues: MultipleIssues = .error(
displayableMessageExtraction: { _ in "MOCK" }
Expand Down
14 changes: 6 additions & 8 deletions Tests/MQTests/TheErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import XCTest
final class TheErrorTests: XCTestCase {

func test_merge_combinesSourceCodeContext() {
var error: Undefined = .error(
file: "file",
line: 42
)

error.merge(
with:
Unimplemented
let error: MultipleIssues = .error(
collecting: Undefined.error(
file: "file",
line: 42
),
Unimplemented
.error(
file: "other_file",
line: 0
Expand Down

0 comments on commit 924b841

Please sign in to comment.