Skip to content
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
10 changes: 10 additions & 0 deletions Sources/ErrorKit/Helpers/String+ErrorKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ extension String {
}
#endif
}

extension String.StringInterpolation {
mutating public func appendInterpolation(error: some Error) {
appendInterpolation(ErrorKit.userFriendlyMessage(for: error))
}

mutating public func appendInterpolation(errorChain error: some Error) {
appendInterpolation(ErrorKit.errorChainDescription(for: error))
}
}
21 changes: 21 additions & 0 deletions Tests/ErrorKitTests/ErrorKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ enum ErrorKitTests {
let nestedError = DatabaseError.caught(FileError.caught(PermissionError.denied(permission: "~/Downloads/Profile.png")))
#expect(ErrorKit.userFriendlyMessage(for: nestedError) == "Access to ~/Downloads/Profile.png was declined. To use this feature, please enable the permission in your device Settings.")
}

@Test
static func errorStringInterpolation() async throws {
#expect("\(error: SomeThrowable())" == "Something failed hard.")
}

@Test
static func nestedErrorStringInterpolation() async throws {
let nestedError = DatabaseError.caught(FileError.caught(PermissionError.denied(permission: "~/Downloads/Profile.png")))
#expect("\(error: nestedError)" == "Access to ~/Downloads/Profile.png was declined. To use this feature, please enable the permission in your device Settings.")
}

@Test
static func chainedErrorStringInterpolation() async throws {
#expect(
"\(errorChain: SomeLocalizedError())" == """
SomeLocalizedError [Struct]
└─ userFriendlyMessage: "Something failed. It failed because it wanted to. Try again later."
"""
)
}
}

enum ErrorChainDescription {
Expand Down