11import Foundation
22
33/// Represents errors that occur during database operations.
4+ ///
5+ /// # Examples of Use
6+ ///
7+ /// ## Handling Database Connections
8+ /// ```swift
9+ /// struct DatabaseConnection {
10+ /// func connect() throws(DatabaseError) {
11+ /// guard let socket = openNetworkSocket() else {
12+ /// throw .connectionFailed
13+ /// }
14+ /// // Successful connection logic
15+ /// }
16+ /// }
17+ /// ```
18+ ///
19+ /// ## Managing Record Operations
20+ /// ```swift
21+ /// struct UserRepository {
22+ /// func findUser(byId id: String) throws(DatabaseError) -> User {
23+ /// guard let user = database.findUser(id: id) else {
24+ /// throw .recordNotFound(entity: "User", identifier: id)
25+ /// }
26+ /// return user
27+ /// }
28+ ///
29+ /// func updateUser(_ user: User) throws(DatabaseError) {
30+ /// guard hasValidPermissions(for: user) else {
31+ /// throw .operationFailed(context: "Updating user profile")
32+ /// }
33+ /// // Update logic
34+ /// }
35+ /// }
36+ /// ```
437public enum DatabaseError : Throwable {
538 /// The database connection failed.
39+ ///
40+ /// # Example
41+ /// ```swift
42+ /// struct AuthenticationService {
43+ /// func authenticate() throws(DatabaseError) {
44+ /// guard let connection = attemptDatabaseConnection() else {
45+ /// throw .connectionFailed
46+ /// }
47+ /// // Proceed with authentication
48+ /// }
49+ /// }
50+ /// ```
651 case connectionFailed
752
853 /// The database query failed to execute.
54+ ///
55+ /// # Example
56+ /// ```swift
57+ /// struct AnalyticsRepository {
58+ /// func generateReport(for period: DateInterval) throws(DatabaseError) -> Report {
59+ /// guard period.duration <= maximumReportPeriod else {
60+ /// throw .operationFailed(context: "Generating analytics report")
61+ /// }
62+ /// // Report generation logic
63+ /// }
64+ /// }
65+ /// ```
966 /// - Parameters:
1067 /// - context: A description of the operation or entity being queried.
1168 case operationFailed( context: String )
1269
1370 /// A requested record was not found in the database.
71+ ///
72+ /// # Example
73+ /// ```swift
74+ /// struct ProductInventory {
75+ /// func fetchProduct(sku: String) throws(DatabaseError) -> Product {
76+ /// guard let product = database.findProduct(bySKU: sku) else {
77+ /// throw .recordNotFound(entity: "Product", identifier: sku)
78+ /// }
79+ /// return product
80+ /// }
81+ /// }
82+ /// ```
1483 /// - Parameters:
1584 /// - entity: The name of the entity or record type.
1685 /// - identifier: A unique identifier for the missing entity.
1786 case recordNotFound( entity: String , identifier: String ? )
1887
1988 /// Generic error message if the existing cases don't provide the required details.
89+ ///
90+ /// # Example
91+ /// ```swift
92+ /// struct DataMigrationService {
93+ /// func migrate() throws(DatabaseError) {
94+ /// guard canPerformMigration() else {
95+ /// throw .generic(userFriendlyMessage: "Migration cannot be performed")
96+ /// }
97+ /// // Migration logic
98+ /// }
99+ /// }
100+ /// ```
20101 case generic( userFriendlyMessage: String )
21102
22103 /// A user-friendly error message suitable for display to end users.
@@ -25,20 +106,20 @@ public enum DatabaseError: Throwable {
25106 case . connectionFailed:
26107 return String (
27108 localized: " BuiltInErrors.DatabaseError.connectionFailed " ,
28- defaultValue: " Failed to connect to the database. Please try again later ." ,
109+ defaultValue: " Unable to establish a connection to the database. Check your network settings and try again." ,
29110 bundle: . module
30111 )
31112 case . operationFailed( let context) :
32113 return String (
33114 localized: " BuiltInErrors.DatabaseError.operationFailed " ,
34- defaultValue: " An error occurred while performing the operation: \( context) . Please try again . " ,
115+ defaultValue: " The database operation for \( context) could not be completed . Please retry the action ." ,
35116 bundle: . module
36117 )
37118 case . recordNotFound( let entity, let identifier) :
38- let idMessage = identifier. map { " Identifier: \( $0) . " } ?? " "
119+ let idMessage = identifier. map { " with ID \( $0) " } ?? " "
39120 return String (
40121 localized: " BuiltInErrors.DatabaseError.recordNotFound " ,
41- defaultValue: " The \( entity) record could not be found. \( idMessage ) Please check and try again." ,
122+ defaultValue: " The \( entity) record \( idMessage ) was not found in the database. Verify the details and try again." ,
42123 bundle: . module
43124 )
44125 case . generic( let userFriendlyMessage) :
0 commit comments