@@ -15,7 +15,11 @@ import Foundation
1515/// userFriendlyMessage: String(localized: "The business data doesn't meet required criteria")
1616/// )
1717/// }
18- /// // Continue with business logic
18+ ///
19+ /// // Automatically wrap any other errors if needed
20+ /// try GenericError.catch {
21+ /// try validateAdditionalRules(data)
22+ /// }
1923/// }
2024/// }
2125/// ```
@@ -33,7 +37,7 @@ import Foundation
3337/// }
3438/// }
3539/// ```
36- public struct GenericError : Throwable {
40+ public struct GenericError : Throwable , Catching {
3741 /// A user-friendly message describing the error.
3842 public let userFriendlyMessage : String
3943
@@ -56,4 +60,30 @@ public struct GenericError: Throwable {
5660 public init ( userFriendlyMessage: String ) {
5761 self . userFriendlyMessage = userFriendlyMessage
5862 }
63+
64+ /// Creates a new generic error that wraps another error.
65+ /// Used internally by the ``catch(_:)`` function to automatically wrap any thrown errors.
66+ ///
67+ /// # Example
68+ /// ```swift
69+ /// struct FileProcessor {
70+ /// func processUserData() throws(GenericError) {
71+ /// // Explicit throwing for validation
72+ /// guard isValidPath(userDataPath) else {
73+ /// throw GenericError(userFriendlyMessage: "Invalid file path selected")
74+ /// }
75+ ///
76+ /// // Automatically wrap any file system or JSON errors
77+ /// let userData = try GenericError.catch {
78+ /// let data = try Data(contentsOf: userDataPath)
79+ /// return try JSONDecoder().decode(UserData.self, from: data)
80+ /// }
81+ /// }
82+ /// }
83+ /// ```
84+ ///
85+ /// - Parameter error: The error to be wrapped.
86+ public static func caught( _ error: Error ) -> Self {
87+ GenericError ( userFriendlyMessage: ErrorKit . userFriendlyMessage ( for: error) )
88+ }
5989}
0 commit comments