From ff126c15431a01244cd1507c5e0a7edce70dff78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 11 Oct 2021 16:24:38 +0200 Subject: [PATCH] SE-0276 Multi-Pattern Catch Clauses --- Documentation/SharingADatabase.md | 4 ++-- README.md | 9 +++------ Tests/GRDBTests/DatabaseConfigurationTests.swift | 4 ++-- Tests/GRDBTests/DatabaseReaderTests.swift | 2 +- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/Documentation/SharingADatabase.md b/Documentation/SharingADatabase.md index 429ead152..9e0d1206f 100644 --- a/Documentation/SharingADatabase.md +++ b/Documentation/SharingADatabase.md @@ -161,7 +161,7 @@ With such a setup, you may still get `SQLITE_BUSY` (5, "database is locked") err ```swift do { try dbPool.write { db in ... } -} catch let error as DatabaseError where error.resultCode == .SQLITE_BUSY { +} catch DatabaseError.SQLITE_BUSY { // Another process won't let you write. Deal with it. } ``` @@ -251,7 +251,7 @@ See https://developer.apple.com/documentation/xcode/understanding-the-exception- ```swift do { try dbPool.write { db in ... } - } catch let error as DatabaseError where error.isInterruptionError { + } catch DatabaseError.SQLITE_INTERRUPT, DatabaseError.SQLITE_ABORT { // Oops, the database is suspended. // Maybe try again after database is resumed? } diff --git a/README.md b/README.md index d8437a0aa..e4ddffc4b 100644 --- a/README.md +++ b/README.md @@ -6909,12 +6909,12 @@ try dbQueue.write { db in } // throws SQLITE_ABORT ``` -You can catch both `SQLITE_INTERRUPT` and `SQLITE_ABORT` errors with the `DatabaseError.isInterruptionError` property: +You can catch both `SQLITE_INTERRUPT` and `SQLITE_ABORT` errors: ```swift do { try dbPool.write { db in ... } -} catch let error as DatabaseError where error.isInterruptionError { +} catch DatabaseError.SQLITE_INTERRUPT, DatabaseError.SQLITE_ABORT { // Oops, the database was interrupted. } ``` @@ -8366,10 +8366,7 @@ When your application should be able to run in the background on a locked device ```swift do { try ... -} catch let error as DatabaseError where - error.resultCode == .SQLITE_IOERR || - error.resultCode == .SQLITE_AUTH -{ +} catch DatabaseError.SQLITE_IOERR, DatabaseError.SQLITE_AUTH { // Handle possible data protection error } ``` diff --git a/Tests/GRDBTests/DatabaseConfigurationTests.swift b/Tests/GRDBTests/DatabaseConfigurationTests.swift index 1d9030aaf..9db22be08 100644 --- a/Tests/GRDBTests/DatabaseConfigurationTests.swift +++ b/Tests/GRDBTests/DatabaseConfigurationTests.swift @@ -190,7 +190,7 @@ class DatabaseConfigurationTests: GRDBTestCase { _ = s2.wait(timeout: .distantFuture) try dbQueue2.inTransaction(.exclusive) { db in return .commit } XCTFail("Expected error") - } catch let error as DatabaseError where error.resultCode == .SQLITE_BUSY { + } catch DatabaseError.SQLITE_BUSY { } catch { XCTFail("\(error)") } @@ -240,7 +240,7 @@ class DatabaseConfigurationTests: GRDBTestCase { // _ = s2.wait(timeout: .distantFuture) // try dbQueue2.inTransaction(.exclusive) { db in return .commit } // XCTFail("Expected error") -// } catch let error as DatabaseError where error.resultCode == .SQLITE_BUSY { +// } catch DatabaseError.SQLITE_BUSY { // } catch { // XCTFail("\(error)") // } diff --git a/Tests/GRDBTests/DatabaseReaderTests.swift b/Tests/GRDBTests/DatabaseReaderTests.swift index a8e1d4455..00d941c29 100644 --- a/Tests/GRDBTests/DatabaseReaderTests.swift +++ b/Tests/GRDBTests/DatabaseReaderTests.swift @@ -37,7 +37,7 @@ class DatabaseReaderTests : GRDBTestCase { try db.execute(sql: "CREATE TABLE t (id INTEGER PRIMARY KEY)") } XCTFail("Expected error") - } catch let error as DatabaseError where error.resultCode == .SQLITE_READONLY { + } catch DatabaseError.SQLITE_READONLY { } }