Skip to content

Commit 4dee83d

Browse files
authored
Merge pull request #37 from finestructure/add-string-interpolation
Add StringInterpolation helpers to render errors
2 parents 2c1aa89 + 9edbd2c commit 4dee83d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Sources/ErrorKit/Helpers/String+ErrorKit.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ extension String {
1717
}
1818
#endif
1919
}
20+
21+
extension String.StringInterpolation {
22+
mutating public func appendInterpolation(error: some Error) {
23+
appendInterpolation(ErrorKit.userFriendlyMessage(for: error))
24+
}
25+
26+
mutating public func appendInterpolation(errorChain error: some Error) {
27+
appendInterpolation(ErrorKit.errorChainDescription(for: error))
28+
}
29+
}

Tests/ErrorKitTests/ErrorKitTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@ enum ErrorKitTests {
3838
let nestedError = DatabaseError.caught(FileError.caught(PermissionError.denied(permission: "~/Downloads/Profile.png")))
3939
#expect(ErrorKit.userFriendlyMessage(for: nestedError) == "Access to ~/Downloads/Profile.png was declined. To use this feature, please enable the permission in your device Settings.")
4040
}
41+
42+
@Test
43+
static func errorStringInterpolation() async throws {
44+
#expect("\(error: SomeThrowable())" == "Something failed hard.")
45+
}
46+
47+
@Test
48+
static func nestedErrorStringInterpolation() async throws {
49+
let nestedError = DatabaseError.caught(FileError.caught(PermissionError.denied(permission: "~/Downloads/Profile.png")))
50+
#expect("\(error: nestedError)" == "Access to ~/Downloads/Profile.png was declined. To use this feature, please enable the permission in your device Settings.")
51+
}
52+
53+
@Test
54+
static func chainedErrorStringInterpolation() async throws {
55+
#expect(
56+
"\(errorChain: SomeLocalizedError())" == """
57+
SomeLocalizedError [Struct]
58+
└─ userFriendlyMessage: "Something failed. It failed because it wanted to. Try again later."
59+
"""
60+
)
61+
}
4162
}
4263

4364
enum ErrorChainDescription {

0 commit comments

Comments
 (0)