Skip to content

Commit 1bdd7f3

Browse files
committed
Docs: how to get error message
Closes stephencelis#366
1 parent 1602cc0 commit 1bdd7f3

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

Documentation/Index.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,21 +650,26 @@ follow similar patterns.
650650
> // INSERT INTO "timestamps" DEFAULT VALUES
651651
> ```
652652

653-
### Handling specific SQLite errors
653+
### Handling SQLite errors
654654

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)):
656657

657658
```swift
658659
do {
659660
try db.run(users.insert(email <- "alice@mac.com"))
660661
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)")
663664
} catch let error {
664665
print("insertion failed: \(error)")
665666
}
666667
```
667668

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+
668673
### Setters
669674

670675
SQLite.swift typically uses the `<-` operator to set values during [inserts

Sources/SQLite/Core/Connection.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,13 @@ public enum Result : Error {
681681

682682
fileprivate static let successCodes: Set = [SQLITE_OK, SQLITE_ROW, SQLITE_DONE]
683683

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
684691
case error(message: String, code: Int32, statement: Statement?)
685692

686693
init?(errorCode: Int32, connection: Connection, statement: Statement? = nil) {

0 commit comments

Comments
 (0)