Skip to content

Commit 44452ad

Browse files
committed
Document error handling
Closes stephencelis#700
1 parent 0fa5e5e commit 44452ad

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Documentation/Index.md

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

653+
### Handling specific SQLite errors
654+
655+
You can pattern match on the error to selectively catch SQLite errors:
656+
657+
```swift
658+
do {
659+
try db.run(users.insert(email <- "[email protected]"))
660+
try db.run(users.insert(email <- "[email protected]"))
661+
} catch let Result.error(_, code, _) where code == SQLITE_CONSTRAINT {
662+
print("constraint failed")
663+
} catch let error {
664+
print("insertion failed: \(error)")
665+
}
666+
```
653667

654668
### Setters
655669

Tests/SQLiteTests/QueryTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,4 +518,16 @@ class QueryIntegrationTests : SQLiteTestCase {
518518
}
519519
}
520520
}
521+
522+
func test_catchConstraintError() {
523+
try! db.run(users.insert(email <- "[email protected]"))
524+
do {
525+
try db.run(users.insert(email <- "[email protected]"))
526+
XCTFail("expected error")
527+
} catch let Result.error(_, code, _) where code == SQLITE_CONSTRAINT {
528+
// expected
529+
} catch let error {
530+
XCTFail("unexpected error: \(error)")
531+
}
532+
}
521533
}

0 commit comments

Comments
 (0)