Improve error message

This commit is contained in:
Gwendal Roué authored and Gwendal Roué committed 2025-09-23 17:12:27 +02:00
1 parent c7bbebc80f
commit 9518d74fa6
3 files changed
+75 -15

No files matched your search

+28 -5
View File
@@ -347,7 +347,22 @@ extension Database {
return result
}
}
throw DatabaseError.noSuchTable(tableName)
if (try? viewExists(tableName, in: schemaName)) == true {
if configuration.schemaSource == nil {
throw DatabaseError(message: """
The database view '\(tableName)' has no primary key. \
To support views, provide a custom schema source in Configuration.schemaSource.
""")
} else {
throw DatabaseError(message: """
The database view '\(tableName)' has no primary key, \
according to Configuration.schemaSource.
""")
}
} else {
throw DatabaseError.noSuchTable(tableName)
}
}
/// Returns the name of the single-column primary key.
@@ -370,10 +385,18 @@ extension Database {
if case .SQLITE_ERROR = error.resultCode,
(try? viewExists(tableName)) == true
{
fatalError("""
Filtering by primary key is not available on the database view '\(tableName)'. \
Use `filter(Column("...") == value)` instead.
""")
if configuration.schemaSource == nil {
fatalError("""
Filtering by primary key is not available on the database view '\(tableName)'. \
Instead, use `filter(Column("...") == value)`, or provide \
a custom schema source in Configuration.schemaSource.
""")
} else {
fatalError("""
Filtering by primary key requires a single-column primary key in the view '\(tableName)'. \
No such primary key is configured by Configuration.schemaSource.
""")
}
} else {
throw error
}
@@ -826,10 +826,27 @@ extension TableRequest where Self: FilteredRequest, Self: TypedRequest {
// ("foo", "bar") do not contain a unique key (primary key
// or unique index).
guard let columns = try db.columnsForUniqueKey(key.keys, in: databaseTableName) else {
fatalError("""
table \(databaseTableName) has no unique key on column(s) \
\(key.keys.sorted().joined(separator: ", "))
""")
if (try? db.viewExists(databaseTableName)) == true {
if db.configuration.schemaSource == nil {
fatalError("""
view \(databaseTableName) has no unique key on column(s) \
\(key.keys.sorted().joined(separator: ", ")). To support \
keys in views, provide a custom schema source in \
Configuration.schemaSource.
""")
} else {
fatalError("""
view \(databaseTableName) has no unique key on column(s) \
\(key.keys.sorted().joined(separator: ", ")), \
according to Configuration.schemaSource.
""")
}
} else {
fatalError("""
table \(databaseTableName) has no unique key on column(s) \
\(key.keys.sorted().joined(separator: ", "))
""")
}
}
let lowercaseColumns = columns.map { $0.lowercased() }
+26 -6
View File
@@ -26,8 +26,16 @@ class PrimaryKeyInfoTests: GRDBTestCase {
XCTFail("Expected Error")
} catch let error as DatabaseError {
XCTAssertEqual(error.resultCode, .SQLITE_ERROR)
XCTAssertEqual(error.message, "no such table: items")
XCTAssertEqual(error.description, "SQLite error 1: no such table: items")
XCTAssertEqual(error.message, """
The database view 'items' has no primary key. \
To support views, provide a custom schema source \
in Configuration.schemaSource.
""")
XCTAssertEqual(error.description, """
SQLite error 1: The database view 'items' has no primary key. \
To support views, provide a custom schema source \
in Configuration.schemaSource.
""")
}
}
}
@@ -273,8 +281,14 @@ class PrimaryKeyInfoTests: GRDBTestCase {
XCTFail("Expected Error")
} catch let error as DatabaseError {
XCTAssertEqual(error.resultCode, .SQLITE_ERROR)
XCTAssertEqual(error.message, "no such table: playerView")
XCTAssertEqual(error.description, "SQLite error 1: no such table: playerView")
XCTAssertEqual(error.message, """
The database view 'playerView' has no primary key, \
according to Configuration.schemaSource.
""")
XCTAssertEqual(error.description, """
SQLite error 1: The database view 'playerView' has no primary key, \
according to Configuration.schemaSource.
""")
}
}
}
@@ -307,8 +321,14 @@ class PrimaryKeyInfoTests: GRDBTestCase {
XCTFail("Expected Error")
} catch let error as DatabaseError {
XCTAssertEqual(error.resultCode, .SQLITE_ERROR)
XCTAssertEqual(error.message, "no such table: playerView")
XCTAssertEqual(error.description, "SQLite error 1: no such table: playerView")
XCTAssertEqual(error.message, """
The database view 'playerView' has no primary key, \
according to Configuration.schemaSource.
""")
XCTAssertEqual(error.description, """
SQLite error 1: The database view 'playerView' has no primary key, \
according to Configuration.schemaSource.
""")
}
}
}