File tree Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -650,21 +650,26 @@ follow similar patterns.
650
650
> // INSERT INTO "timestamps" DEFAULT VALUES
651
651
> ` ` `
652
652
653
- # ## Handling specific SQLite errors
653
+ # ## Handling SQLite errors
654
654
655
- You can pattern match on the error to selectively catch SQLite errors:
655
+ You can pattern match on the error to selectively catch SQLite errors. For example, to
656
+ specifically handle constraint errors ([SQLITE_CONSTRAINT ](https: // sqlite.org/ rescode.html# constraint)):
656
657
657
658
` ` ` swift
658
659
do {
659
660
try db.run(users.insert(email <- "alice@mac.com"))
660
661
try db.run(users.insert(email <- "alice@mac.com"))
661
- } catch let Result.error(_ , code, _ ) where code == SQLITE_CONSTRAINT {
662
- print("constraint failed")
662
+ } catch let Result.error(message , code, statement ) where code == SQLITE_CONSTRAINT {
663
+ print("constraint failed: \( message), in \( statement) ")
663
664
} catch let error {
664
665
print("insertion failed: \( error)")
665
666
}
666
667
` ` `
667
668
669
+ The ` Result.error` type contains the English - language text that describes the error (` message` ),
670
+ the error ` code` (see [SQLite result code list](https: // sqlite.org/ rescode.html# primary_result_code_list)
671
+ for details) and a optional reference to the ` statement` which produced the error.
672
+
668
673
# ## Setters
669
674
670
675
SQLite .swift typically uses the ` <-` operator to set values during [inserts
Original file line number Diff line number Diff line change @@ -681,6 +681,13 @@ public enum Result : Error {
681
681
682
682
fileprivate static let successCodes : Set = [ SQLITE_OK, SQLITE_ROW, SQLITE_DONE]
683
683
684
+ /// Represents a SQLite specific [error code](https://sqlite.org/rescode.html)
685
+ ///
686
+ /// - message: English-language text that describes the error
687
+ ///
688
+ /// - code: SQLite [error code](https://sqlite.org/rescode.html#primary_result_code_list)
689
+ ///
690
+ /// - statement: the statement which produced the error
684
691
case error( message: String , code: Int32 , statement: Statement ? )
685
692
686
693
init ? ( errorCode: Int32 , connection: Connection , statement: Statement ? = nil ) {
You can’t perform that action at this time.
0 commit comments