Skip to content

Commit 861f267

Browse files
authored
Merge pull request #46 from roanutil/fix-incorrect-error-descriptions
Fix incorrect error descriptions
2 parents 47de46c + b91ae75 commit 861f267

5 files changed

Lines changed: 22 additions & 12 deletions

File tree

Sources/CoreDataRepository/FetchableUnmanagedModel.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public protocol FetchableUnmanagedModel: Sendable {
7070

7171
/// A description of the context from where an error is thrown
7272
var errorDescription: String { get }
73+
74+
/// A description of the context from where an error is thrown but there is no instance `self` to use
75+
static var errorDescription: String { get }
7376
}
7477

7578
extension FetchableUnmanagedModel {
@@ -85,4 +88,9 @@ extension FetchableUnmanagedModel {
8588
public var errorDescription: String {
8689
"\(Self.self)"
8790
}
91+
92+
@inlinable
93+
public static var errorDescription: String {
94+
"\(Self.self)"
95+
}
8896
}

Sources/CoreDataRepository/IdentifiedUnmanagedModel.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,22 @@ extension IdentifiedUnmanagedModel {
3232
let fetchResult = try context.fetch(request)
3333
guard let managed = fetchResult.first, fetchResult.count == 1 else {
3434
throw CoreDataError
35-
.noMatchFoundWhenReadingItem(description: "\(Self.self) -- id: \(errorDescription(for: id))")
35+
.noMatchFoundWhenReadingItem(
36+
description: "\(Self.errorDescription) -- id: \(errorDescription(for: id))"
37+
)
3638
}
3739
guard !managed.isDeleted else {
3840
throw CoreDataError
39-
.fetchedObjectIsFlaggedAsDeleted(description: "\(Self.self) -- id: \(errorDescription(for: id))")
41+
.fetchedObjectIsFlaggedAsDeleted(
42+
description: "\(Self.errorDescription) -- id: \(errorDescription(for: id))"
43+
)
4044
}
4145
return managed
4246
}
4347
}
48+
49+
extension IdentifiedUnmanagedModel where UnmanagedId: CustomStringConvertible {
50+
public static func errorDescription(for unmanagedId: UnmanagedId) -> String {
51+
unmanagedId.description
52+
}
53+
}

Sources/CoreDataRepository/ReadableUnmanagedModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ extension ReadableUnmanagedModel where Self: ManagedIdReferencable {
8080
@inlinable
8181
public func readManaged(from context: NSManagedObjectContext) throws -> ManagedModel {
8282
guard let managedId else {
83-
throw CoreDataError.noObjectIdOnItem(description: "\(Self.self)")
83+
throw CoreDataError.noObjectIdOnItem(description: errorDescription)
8484
}
8585
return try context.notDeletedObject(for: managedId).asManagedModel()
8686
}
@@ -90,7 +90,7 @@ extension ReadableUnmanagedModel where Self: ManagedIdUrlReferencable {
9090
@inlinable
9191
public func readManaged(from context: NSManagedObjectContext) throws -> ManagedModel {
9292
guard let managedIdUrl else {
93-
throw CoreDataError.noUrlOnItemToMapToObjectId(description: "\(Self.self)")
93+
throw CoreDataError.noUrlOnItemToMapToObjectId(description: errorDescription)
9494
}
9595
let managedId = try context.objectId(from: managedIdUrl).get()
9696
return try context.notDeletedObject(for: managedId).asManagedModel()

Sources/Internal/ModelsWithIntId/IdentifiableModel_Int.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ extension IdentifiableModel_IntId: IdentifiedUnmanagedModel {
105105
}
106106

107107
package nonisolated(unsafe) static let unmanagedIdExpression = NSExpression(forKeyPath: \ManagedModel_IntId.id)
108-
109-
package static func errorDescription(for unmanagedId: Int) -> String {
110-
unmanagedId.description
111-
}
112108
}
113109

114110
extension IdentifiableModel_IntId: WritableUnmanagedModel {

Sources/Internal/ModelsWithUuidId/IdentifiableModel_Uuid.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ extension IdentifiableModel_UuidId: IdentifiedUnmanagedModel {
105105
}
106106

107107
package nonisolated(unsafe) static let unmanagedIdExpression = NSExpression(forKeyPath: \ManagedModel_UuidId.id)
108-
109-
package static func errorDescription(for unmanagedId: UUID) -> String {
110-
unmanagedId.uuidString
111-
}
112108
}
113109

114110
extension IdentifiableModel_UuidId: WritableUnmanagedModel {

0 commit comments

Comments
 (0)