Skip to content

Commit 197ad48

Browse files
committed
Document string interpolation + add "debug:" alias
1 parent 17843c3 commit 197ad48

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ do {
6767
// Better than localizedDescription, works with any error type
6868
print(ErrorKit.userFriendlyMessage(for: error))
6969
// "You are not connected to the Internet. Please check your connection."
70+
71+
// String interpolation automatically uses userFriendlyMessage(for:)
72+
print("Request failed: \(error)")
7073
}
7174
```
7275

Sources/ErrorKit/ErrorKit.docc/Guides/Enhanced-Error-Descriptions.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ If the error already conforms to `Throwable`, its `userFriendlyMessage` is used.
8181

8282
All enhanced error messages are fully localized using the `String(localized:)` pattern, ensuring users receive messages in their preferred language where available.
8383

84+
### String Interpolation Convenience
85+
86+
ErrorKit enhances Swift's string interpolation to automatically use `userFriendlyMessage(for:)`:
87+
88+
```swift
89+
// Instead of:
90+
showAlert(message: "Save failed: \(ErrorKit.userFriendlyMessage(for: error))")
91+
92+
// You can simply use:
93+
showAlert(message: "Save failed: \(error)")
94+
Text("Could not load data: \(error)")
95+
Logger().info("Sync completed with error: \(error)")
96+
```
97+
98+
This works with any error type — both your custom `Throwable` errors and system errors.
99+
84100
### How It Works
85101

86102
The `userFriendlyMessage(for:)` function follows this process to determine the best error message:

Sources/ErrorKit/ErrorKit.docc/Guides/Error-Chain-Debugging.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ For errors conforming to the `Catching` protocol, you get the complete error wra
6363

6464
Even for errors that don't conform to `Catching`, you still get valuable information since most Swift errors are enums. The error chain description will show you the exact enum case (e.g., `FileError.notFound`), making it easy to search your codebase for the error's origin. This is much better than the default cryptic message you get for enum cases when using `localizedDescription`.
6565

66+
### String Interpolation for Debug Logging
67+
68+
ErrorKit enhances string interpolation for error chain debugging. Use either `chain:` or `debug:` (they're aliases) to get the complete hierarchical description:
69+
70+
```swift
71+
// Instead of:
72+
Logger().error("Update failed: \(ErrorKit.errorChainDescription(for: error))")
73+
74+
// You can simply use either:
75+
Logger().error("Update failed:\n\(chain: error)")
76+
Logger().error("Update failed:\n\(debug: error)")
77+
```
78+
79+
Use `\(error)` for user-facing messages, `\(chain: error)` or `\(debug: error)` for debugging.
80+
6681
### Error Analytics with Grouping IDs
6782

6883
To help prioritize which errors to fix, ErrorKit provides `groupingID(for:)` that generates stable identifiers for errors sharing the exact same type structure and enum cases:

Sources/ErrorKit/Helpers/String+ErrorKit.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,25 @@ extension String.StringInterpolation {
5151
mutating public func appendInterpolation(chain error: some Error) {
5252
self.appendInterpolation(ErrorKit.errorChainDescription(for: error))
5353
}
54+
55+
/// Interpolates an error using its complete chain description for debugging.
56+
///
57+
/// Uses ``ErrorKit.errorChainDescription(for:)`` to show the full error hierarchy,
58+
/// type information, and nested structure. Ideal for logging and debugging.
59+
///
60+
/// ```swift
61+
/// Logger().error("Operation failed with:\n\(chain: error)")
62+
/// // Operation failed with:
63+
/// // DatabaseError
64+
/// // └─ FileError
65+
/// // └─ PermissionError.denied(permission: "~/Downloads/Profile.png")
66+
/// // └─ userFriendlyMessage: "Access to ~/Downloads/Profile.png was declined..."
67+
/// ```
68+
///
69+
/// - Parameter error: The error to interpolate using its complete chain description
70+
///
71+
/// - NOTE: Alias for ``appendInterpolation(chain:)``.
72+
mutating public func appendInterpolation(debug error: some Error) {
73+
self.appendInterpolation(chain: error)
74+
}
5475
}

0 commit comments

Comments
 (0)