Skip to content

Commit fd860bc

Browse files
committed
Add a custom mappers section to the enhanced error descriptions article
1 parent f0d891f commit fd860bc

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

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

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,18 @@ do {
7575
}
7676
```
7777

78-
If the error already conforms to `Throwable`, its `userFriendlyMessage` is used. For system errors, ErrorKit provides an enhanced description from its built-in mappings.
78+
If the error already conforms to `Throwable`, its `userFriendlyMessage` is used. For system errors, ErrorKit provides an enhanced description from its built-in mappers.
7979

8080
### Localization Support
8181

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

8484
### How It Works
8585

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

8888
1. If the error conforms to `Throwable`, it uses the error's own `userFriendlyMessage`
89-
2. It tries domain-specific handlers to find available enhanced versions
89+
2. It queries registered error mappers to find enhanced descriptions
9090
3. If the error conforms to `LocalizedError`, it combines its localized properties
9191
4. As a fallback, it formats the NSError domain and code along with the standard `localizedDescription`
9292

@@ -95,32 +95,57 @@ The `userFriendlyMessage(for:)` function follows this process to determine the b
9595
You can help improve ErrorKit by contributing better error descriptions for common error types:
9696

9797
1. Identify cryptic error messages from system frameworks
98-
2. Implement domain-specific handlers or extend existing ones (see folder `EnhancedDescriptions`)
98+
2. Implement domain-specific handlers or extend existing ones (see folder `ErrorMappers`)
9999
3. Use clear, actionable language that helps users understand what went wrong
100100
4. Include localization support for all messages (no need to actually localize, we'll take care)
101101

102102
Example contribution to handle a new error type:
103103

104104
```swift
105-
// In ErrorKit+Foundation.swift
105+
// In FoundationErrorMapper.swift
106106
case let jsonError as NSError where jsonError.domain == NSCocoaErrorDomain && jsonError.code == 3840:
107-
return String.localized(
108-
key: "EnhancedDescriptions.JSONError.invalidFormat",
109-
defaultValue: "The data couldn't be read because it isn't in the correct format."
110-
)
107+
return String(localized: "The data couldn't be read because it isn't in the correct format.")
111108
```
112109

110+
### Custom Error Mappers
111+
112+
While ErrorKit focuses on enhancing system and framework errors, you can also create custom mappers for any library:
113+
114+
```swift
115+
enum MyLibraryErrorMapper: ErrorMapper {
116+
static func userFriendlyMessage(for error: Error) -> String? {
117+
switch error {
118+
case let libraryError as MyLibrary.Error:
119+
switch libraryError {
120+
case .apiKeyExpired:
121+
return String(localized: "API key expired. Please update your credentials.")
122+
default:
123+
return nil
124+
}
125+
default:
126+
return nil
127+
}
128+
}
129+
}
130+
131+
// On app start:
132+
ErrorKit.registerMapper(MyLibraryErrorMapper.self)
133+
```
134+
135+
This extensibility allows the community to create mappers for 3rd-party libraries with known error issues.
136+
113137
## Topics
114138

115139
### Essentials
116140

117141
- ``ErrorKit/userFriendlyMessage(for:)``
142+
- ``ErrorMapper``
118143

119-
### Domain-Specific Handlers
144+
### Built-in Mappers
120145

121-
- ``ErrorKit/userFriendlyFoundationMessage(for:)``
122-
- ``ErrorKit/userFriendlyCoreDataMessage(for:)``
123-
- ``ErrorKit/userFriendlyMapKitMessage(for:)``
146+
- ``FoundationErrorMapper``
147+
- ``CoreDataErrorMapper``
148+
- ``MapKitErrorMapper``
124149

125150
### Continue Reading
126151

0 commit comments

Comments
 (0)